package com.hzya.frame.mdm.mdmModule.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableField;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.beanutil.BeanUtil;
import com.hzya.frame.mdm.entity.MdmDbFiledVo;
import com.hzya.frame.mdm.entity.MdmDto;
import com.hzya.frame.mdm.entity.MdmModuleViewVo;
import com.hzya.frame.mdm.entity.MdmQuery;
import com.hzya.frame.mdm.mdmModule.entity.MdmModuleEntity;
import com.hzya.frame.mdm.mdmModule.dao.IMdmModuleDao;
import com.hzya.frame.mdm.mdmModule.service.IMdmModuleService;
import com.hzya.frame.mdm.mdmModule.vo.ExcelTemplateVO;
import com.hzya.frame.mdm.mdmModule.vo.ImportExcelVO;
import com.hzya.frame.mdm.mdmModuleDb.dao.IMdmModuleDbDao;
import com.hzya.frame.mdm.mdmModuleDb.dao.impl.MdmModuleDbDaoImpl;
import com.hzya.frame.mdm.mdmModuleDb.entity.MdmModuleDbEntity;
import com.hzya.frame.mdm.mdmModuleDbFileds.dao.IMdmModuleDbFiledsDao;
import com.hzya.frame.mdm.mdmModuleDbFileds.entity.MdmModuleDbFiledsEntity;
import com.hzya.frame.mdm.mdmModuleDbFiledsRule.dao.IMdmModuleDbFiledsRuleDao;
import com.hzya.frame.mdm.mdmModuleDbFiledsRule.entity.MdmModuleDbFiledsRuleEntity;
import com.hzya.frame.mdm.mdmModuleDistribute.dao.IMdmModuleDistributeDao;
import com.hzya.frame.mdm.mdmModuleDistribute.entity.MdmModuleDistributeEntity;
import com.hzya.frame.mdm.mdmModuleDistributeDetail.dao.IMdmModuleDistributeDetailDao;
import com.hzya.frame.mdm.mdmModuleDistributeDetail.entity.MdmModuleDistributeDetailEntity;
import com.hzya.frame.mdm.mdmModuleOptionLog.dao.IMdmModuleOptionLogDao;
import com.hzya.frame.mdm.mdmModuleOptionLog.entity.MdmModuleOptionLogEntity;
import com.hzya.frame.mdm.mdmModuleSendLog.dao.IMdmModuleSendLogDao;
import com.hzya.frame.mdm.mdmModuleSendLog.entity.MdmModuleSendLogEntity;
import com.hzya.frame.mdm.mdmModuleSource.dao.IMdmModuleSourceDao;
import com.hzya.frame.mdm.mdmModuleSource.entity.MdmModuleSourceEntity;
import com.hzya.frame.mdm.mdmModuleView.dao.IMdmModuleViewDao;
import com.hzya.frame.mdm.mdmModuleView.entity.MdmModuleViewEntity;
import com.hzya.frame.mdm.mdmModuleViewButton.dao.IMdmModuleViewButtonDao;
import com.hzya.frame.mdm.mdmModuleViewButton.entity.MdmModuleViewButtonEntity;
import com.hzya.frame.mdm.mdmModuleViewDetail.dao.IMdmModuleViewDetailDao;
import com.hzya.frame.mdm.mdmModuleViewDetail.entity.MdmModuleViewDetailEntity;
import com.hzya.frame.mdm.mdmTableCodeRule.dao.IMdmTableCodeRuleDao;
import com.hzya.frame.mdm.mdmTableCodeRule.dao.impl.MdmTableCodeRuleDaoImpl;
import com.hzya.frame.mdm.mdmTableCodeRule.entity.MdmTableCodeRuleEntity;
import com.hzya.frame.mdm.service.IMdmServiceCache;
import com.hzya.frame.mdm.service.impl.MdmServiceCache;
import com.hzya.frame.sysnew.buttonConfig.dao.ISysButtonConfigDao;
import com.hzya.frame.sysnew.buttonConfig.entity.SysButtonConfigEntity;
import com.hzya.frame.sysnew.menuConfig.dao.ISysMenuConfigDao;
import com.hzya.frame.sysnew.menuConfig.entity.SysMenuConfigEntity;
import com.hzya.frame.sysnew.popedomMenu.dao.ISysPopedomMenuDao;
import com.hzya.frame.sysnew.popedomMenu.entity.SysPopedomMenuEntity;
import com.hzya.frame.sysnew.popedomOperate.dao.ISysPopedomOperateDao;
import com.hzya.frame.sysnew.popedomOperate.entity.SysPopedomOperateEntity;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import com.hzya.frame.web.exception.BaseSystemException;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.hzya.frame.basedao.service.impl.BaseService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;

/**
 * 主数据模版(MdmModule)表服务实现类
 *
 * @author makejava
 * @since 2024-06-03 08:46:29
 */
@Service(value = "mdmModuleService")
public class MdmModuleServiceImpl extends BaseService<MdmModuleEntity, String> implements IMdmModuleService {

    private IMdmModuleDao mdmModuleDao;
    @Resource
    private ISysPopedomMenuDao sysPopedomMenuDao;
    @Resource
    private ISysPopedomOperateDao sysPopedomOperateDao;
    @Resource
    private ISysButtonConfigDao sysButtonConfigDao;
    @Resource
    private ISysMenuConfigDao sysMenuConfigDao;

    @Resource
    private IMdmModuleSendLogDao mdmModuleSendLogDao;
    @Resource
    private IMdmModuleOptionLogDao mdmModuleOptionLogDao;
    @Resource
    private IMdmServiceCache mdmServiceCache;
    @Resource
    private IMdmModuleDbDao mdmModuleDbDao;
    @Resource
    private IMdmModuleDbFiledsDao mdmModuleDbFiledsDao;
    @Resource
    private IMdmModuleDbFiledsRuleDao mdmModuleDbFiledsRuleDao;
    @Resource
    private IMdmModuleDistributeDao mdmModuleDistributeDao;
    @Resource
    private IMdmModuleDistributeDetailDao mdmModuleDistributeDetailDao;
    @Resource
    private IMdmModuleViewDao mdmModuleViewDao;

    @Resource
    private IMdmModuleSourceDao mdmModuleSourceDao;
    @Resource
    private IMdmModuleViewButtonDao mdmModuleViewButtonDao;
    @Resource
    private IMdmModuleViewDetailDao mdmModuleViewDetailDao;
    @Resource
    private IMdmTableCodeRuleDao mdmTableCodeRuleDao;

    @Autowired
    public void setMdmModuleDao(IMdmModuleDao dao) {
        this.mdmModuleDao = dao;
        this.dao = dao;
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询所有主数据
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity queryMdm(JSONObject object) {
        MdmModuleEntity entity = getData("jsonStr", object, MdmModuleEntity.class);
        List<MdmModuleEntity> mdmModuleEntities = mdmModuleDao.queryByLike(entity);
        return BaseResult.getSuccessMessageEntity("查询所有主数据", mdmModuleEntities);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 删除主数据  校验主表是不是有数据，有数据不允许删除
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity deleteMdm(JSONObject object) {
        MdmModuleEntity entity = getData("jsonStr", object, MdmModuleEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        MdmModuleEntity mdmModuleEntity = mdmModuleDao.get(entity.getId());
        if (mdmModuleEntity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        //查询数据源表
        MdmModuleDbEntity queryDb = new MdmModuleDbEntity();
        queryDb.setMdmId(mdmModuleEntity.getId());
        queryDb.setSts("Y");
        queryDb.setDbType("1");//主表
        List<MdmModuleDbEntity> mdmModuleDbEntities = mdmModuleDbDao.queryBase(queryDb);
        if (mdmModuleDbEntities != null && mdmModuleDbEntities.size() == 1) {
            MdmQuery mdmQuery = new MdmQuery();
            mdmQuery.setTableName(mdmModuleDbEntities.get(0).getDbName());
            Integer count = mdmModuleDao.queryMainCount(mdmQuery);
            if (count > 0) {
                return BaseResult.getFailureMessageEntity(mdmModuleEntity.getMdmName() + "存在数据，不允许删除");
            }
            MdmModuleDbFiledsRuleEntity queryCount = new MdmModuleDbFiledsRuleEntity();
            queryCount.setMdmId(entity.getId());
            queryCount.setRuleCode("service");
            queryCount.setRuleValue(mdmModuleDbEntities.get(0).getDbName());
            Integer counts = mdmModuleDbFiledsRuleDao.queryUserMdm(queryCount);
            if (counts > 0) {
                return BaseResult.getFailureMessageEntity(mdmModuleEntity.getMdmName() + "已被引用，不允许删除");
            }
        }


        //删除单据规则
        MdmTableCodeRuleEntity mdmTableCodeRuleEntity = new MdmTableCodeRuleEntity();
        mdmTableCodeRuleEntity.setUpdate();
        mdmTableCodeRuleEntity.setMdmId(mdmModuleEntity.getId());
        mdmTableCodeRuleDao.logicRemoveMultiCondition(mdmTableCodeRuleEntity);

        //删除视图设置的字段
        MdmModuleViewDetailEntity mdmModuleViewDetailEntity = new MdmModuleViewDetailEntity();
        mdmModuleViewDetailEntity.setUpdate();
        mdmModuleViewDetailEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleViewDetailDao.logicRemoveMultiCondition(mdmModuleViewDetailEntity);

        //删除设置按钮
        MdmModuleViewButtonEntity mdmModuleViewButtonEntity = new MdmModuleViewButtonEntity();
        mdmModuleViewButtonEntity.setUpdate();
        mdmModuleViewButtonEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleViewButtonDao.logicRemoveMultiCondition(mdmModuleViewButtonEntity);

        //删除显示信息
        MdmModuleViewEntity mdmModuleViewEntity = new MdmModuleViewEntity();
        mdmModuleViewEntity.setUpdate();
        mdmModuleViewEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleViewDao.logicRemoveMultiCondition(mdmModuleViewEntity);
        //删除数据来源配置
        MdmModuleSourceEntity mdmModuleSourceEntity = new MdmModuleSourceEntity();
        mdmModuleSourceEntity.setUpdate();
        mdmModuleSourceEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleSourceDao.logicRemoveMultiCondition(mdmModuleSourceEntity);
        //删除下发配置，以及下发配置的数据权限
        MdmModuleDistributeDetailEntity mdmModuleDistributeDetailEntity = new MdmModuleDistributeDetailEntity();
        mdmModuleDistributeDetailEntity.setUpdate();
        mdmModuleDistributeDetailEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleDistributeDetailDao.logicRemoveMultiCondition(mdmModuleDistributeDetailEntity);

        MdmModuleDistributeEntity mdmModuleDistributeEntity = new MdmModuleDistributeEntity();
        mdmModuleDistributeEntity.setUpdate();
        mdmModuleDistributeEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleDistributeDao.logicRemoveMultiCondition(mdmModuleDistributeEntity);
        //删除字段规则表
        MdmModuleDbFiledsRuleEntity mdmModuleDbFiledsRuleEntity = new MdmModuleDbFiledsRuleEntity();
        mdmModuleDbFiledsRuleEntity.setUpdate();
        mdmModuleDbFiledsRuleEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleDbFiledsRuleDao.logicRemoveMultiCondition(mdmModuleDbFiledsRuleEntity);
        //删除字段表
        mdmServiceCache.deleteMdmModuleDbFileds(mdmModuleEntity.getId(), null);
        //删除设置的db，以及实体表
        MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
        mdmModuleDbEntity.setMdmId(mdmModuleEntity.getId());
        List<MdmModuleDbEntity> mdmModuleDbEntities1 = mdmServiceCache.queryMdmModuleDb(mdmModuleDbEntity);
        if (mdmModuleDbEntities1 != null && mdmModuleDbEntities1.size() > 0) {
            for (int i = 0; i < mdmModuleDbEntities1.size(); i++) {
                Map<String, Object> maps = new HashMap<>();
                maps.put("tableName", mdmModuleDbEntities1.get(i).getDbName());
                mdmModuleDao.deleteTable(maps);
            }
        }
        mdmServiceCache.deleteMdmModuleDb(mdmModuleEntity.getId(), null);

        //删除菜单和按钮，以及权限分配（用户权限和角色权限）
        SysMenuConfigEntity sysMenuConfigEntity = new SysMenuConfigEntity();
        sysMenuConfigEntity.setFormId(mdmModuleEntity.getId());
        sysMenuConfigEntity.setSts("Y");
        List<SysMenuConfigEntity> sysMenuConfigEntities = sysMenuConfigDao.queryBase(sysMenuConfigEntity);
        if (sysMenuConfigEntities != null && sysMenuConfigEntities.size() > 0) {
            for (int i = 0; i < sysMenuConfigEntities.size(); i++) {
                SysButtonConfigEntity sysButtonConfigEntity = new SysButtonConfigEntity();
                sysButtonConfigEntity.setMenuId(sysMenuConfigEntities.get(i).getId());
                sysButtonConfigEntity.setSts("Y");
                sysButtonConfigEntity.setUpdate();
                sysButtonConfigDao.logicRemoveMultiCondition(sysButtonConfigEntity);

                SysPopedomOperateEntity sysPopedomOperateEntity = new SysPopedomOperateEntity();
                sysPopedomOperateEntity.setMenuId(sysMenuConfigEntities.get(i).getId());
                sysPopedomOperateEntity.setSts("Y");
                sysPopedomOperateEntity.setUpdate();
                sysPopedomOperateDao.logicRemoveMultiCondition(sysPopedomOperateEntity);

                SysPopedomMenuEntity sysPopedomMenuEntity = new SysPopedomMenuEntity();
                sysPopedomMenuEntity.setMenuId(sysMenuConfigEntities.get(i).getId());
                sysPopedomMenuEntity.setSts("Y");
                sysPopedomMenuEntity.setUpdate();
                sysPopedomMenuDao.logicRemoveMultiCondition(sysPopedomMenuEntity);
                sysMenuConfigEntities.get(i).setUpdate();
                sysMenuConfigDao.logicRemoveMultiCondition(sysMenuConfigEntities.get(i));
            }
        }


        //删除主数据
        mdmServiceCache.deleteMdmModuleEntity(mdmModuleEntity);

        return BaseResult.getSuccessMessageEntity("删除成功");
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-基本信息获取
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity getMdm(JSONObject object) {
        MdmModuleEntity entity = getData("jsonStr", object, MdmModuleEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        MdmModuleEntity mdmModuleEntity = mdmModuleDao.get(entity.getId());
        if (mdmModuleEntity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        return BaseResult.getSuccessMessageEntity("获取成功", mdmModuleEntity);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-基本信息新增
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity saveMdm(JSONObject object) {
        MdmModuleEntity entity = getData("jsonStr", object, MdmModuleEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmName() == null || "".equals(entity.getMdmName())) {
            return BaseResult.getFailureMessageEntity("请先输入主数据名称");
        }
        if (entity.getMdmType() == null || "".equals(entity.getMdmType())) {
            return BaseResult.getFailureMessageEntity("请先输入主数据类型");
        }
        entity.setCreate();
        mdmModuleDao.save(entity);
        entity = mdmModuleDao.get(entity.getId());
        return BaseResult.getSuccessMessageEntity("保存成功", entity);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-基本信息修改（同步修改菜单名称）
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity updateMdm(JSONObject object) {
        MdmModuleEntity entity = getData("jsonStr", object, MdmModuleEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmCode() == null || "".equals(entity.getMdmCode())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity.setUpdate();
        mdmServiceCache.updateMdmModuleEntity(entity);

        //修改菜单的显示信息
        SysMenuConfigEntity sysMenuConfigEntity = new SysMenuConfigEntity();
        sysMenuConfigEntity.setFormId(entity.getId());
        sysMenuConfigEntity.setSts("Y");
        List<SysMenuConfigEntity> sysMenuConfigEntities = sysMenuConfigDao.queryBase(sysMenuConfigEntity);
        if (sysMenuConfigEntities != null && sysMenuConfigEntities.size() > 0) {
            for (int i = 0; i < sysMenuConfigEntities.size(); i++) {

                sysMenuConfigEntities.get(i).setUpdate();
                if ("1".equals(entity.getMdmType())) {
                    //1、档案
                    sysMenuConfigEntities.get(i).setParentMenuId("ea22757e99c144fb9ef381dd322e233c");
                } else {
                    //2、单据
                    sysMenuConfigEntities.get(i).setParentMenuId("67f82a6e7f664c5a89b7fc7b8fc817b0");
                }
                sysMenuConfigEntities.get(i).setMenuName(entity.getMdmName());
                sysMenuConfigEntities.get(i).setMenuEnglishName("integrationOptionAdmin" + entity.getMdmCode());
                JSONObject object1 = JSONObject.parseObject(sysMenuConfigEntities.get(i).getOptions());
                object1.put("mdmId", entity.getId());//主数据id
                object1.put("mdmCode", entity.getMdmCode());//主数据编码
                sysMenuConfigEntities.get(i).setOptions(object1.toString());//菜单mate参数
                sysMenuConfigDao.update(sysMenuConfigEntities.get(i));
            }
        }
        return BaseResult.getSuccessMessageEntity("修改成功");
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-数据源-单据规则获取
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity getMdmTableCodeRule(JSONObject object) {
        MdmTableCodeRuleEntity entity = getData("jsonStr", object, MdmTableCodeRuleEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmId() == null || "".equals(entity.getMdmId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        List<MdmTableCodeRuleEntity> mdmTableCodeRuleEntities = mdmTableCodeRuleDao.queryBase(entity);
        return BaseResult.getSuccessMessageEntity("获取单据规则成功", mdmTableCodeRuleEntities);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-数据源-单据规则新增或修改
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity saveOrUpdateMdmTableCodeRule(JSONObject object) {
        MdmDto entity = getData("jsonStr", object, MdmDto.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmTableCodeRuleEntityList() == null || entity.getMdmTableCodeRuleEntityList().size() == 0) {
            return BaseResult.getFailureMessageEntity("请先设置单据规则");
        }

        MdmTableCodeRuleEntity mdmTableCodeRuleEntity = new MdmTableCodeRuleEntity();
        mdmTableCodeRuleEntity.setMdmId(entity.getId());
        mdmTableCodeRuleEntity.setSts("Y");
        mdmTableCodeRuleEntity.setUpdate();
        mdmTableCodeRuleDao.logicRemoveMultiCondition(mdmTableCodeRuleEntity);
        if (entity.getMdmTableCodeRuleEntityList() != null && entity.getMdmTableCodeRuleEntityList().size() > 0) {
            for (int i = 0; i < entity.getMdmTableCodeRuleEntityList().size(); i++) {
                entity.getMdmTableCodeRuleEntityList().get(i).setMdmId(entity.getId());
                entity.getMdmTableCodeRuleEntityList().get(i).setCreate();
                mdmTableCodeRuleDao.save(entity.getMdmTableCodeRuleEntityList().get(i));
            }
        }

        return BaseResult.getSuccessMessageEntity("保存单据规则成功");
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-数据源-数据表获取
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity queryMdmDb(JSONObject object) {
        MdmModuleDbEntity entity = getData("jsonStr", object, MdmModuleDbEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmId() == null || "".equals(entity.getMdmId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        List<MdmModuleDbEntity> mdmModuleDbEntities = mdmServiceCache.queryMdmModuleDb(entity);
        List<MdmModuleDbEntity> dbEntities = new ArrayList<>();
        if (mdmModuleDbEntities != null && mdmModuleDbEntities.size() > 0) {
            for (int i = 0; i < mdmModuleDbEntities.size(); i++) {
                if ("1".equals(mdmModuleDbEntities.get(i).getDbType()) || "2".equals(mdmModuleDbEntities.get(i).getDbType())) {
                    dbEntities.add(mdmModuleDbEntities.get(i));
                    //查询合并规则。add by zyd 20250707
                    List<String> filedNames=new ArrayList<>();
                    if (mdmModuleDbEntities.get(i).getMergeRules() != null && !"".equals(mdmModuleDbEntities.get(i).getMergeRules())) {
                        String mergeRulesIds = mdmModuleDbEntities.get(i).getMergeRules();
                        String[] split = mergeRulesIds.split(",");
                        for (String filedId : split) {
                            MdmModuleDbFiledsEntity mdmModuleDbFiledsEntity = new MdmModuleDbFiledsEntity();
                            mdmModuleDbFiledsEntity.setId(filedId);
                            List<MdmModuleDbFiledsEntity> fileds = mdmModuleDbFiledsDao.query(mdmModuleDbFiledsEntity);
                            if(fileds.size()!=0){
                                filedNames.add(fileds.get(0).getChName());
                            }

                        }
                    }
                    mdmModuleDbEntities.get(i).setMergeRulesStr(String.join(",",filedNames));
                }
            }
        }
        return BaseResult.getSuccessMessageEntity("获取数据源表成功", dbEntities);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-数据源-数据表新增
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity saveMdmDb(JSONObject object) {
        MdmModuleDbEntity entity = getData("jsonStr", object, MdmModuleDbEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmId() == null || "".equals(entity.getMdmId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        List<MdmModuleDbEntity> mdmModuleDbEntities = new ArrayList<>();
        List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntities = new ArrayList<>();
        entity.setDataType("1");
        //保存数据表，新建默认字段
        entity.setCreate();
        mdmModuleDbEntities.add(entity);
        setMdmField(entity, mdmModuleDbFiledsEntities);
        //如果是主表，同步新建 两张日志表,添加日志表字段
        if ("1".equals(entity.getDbType())) {
            MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
            mdmModuleDbEntity.setMdmId(entity.getMdmId());
            mdmModuleDbEntity.setDbName(entity.getDbName() + "_option_log");
            mdmModuleDbEntity.setDbType("3");
            mdmModuleDbEntity.setRemark(entity.getRemark() + "操作日志");
            mdmModuleDbEntity.setCreate();
            mdmModuleDbEntity.setDataType("1");
            mdmModuleDbEntities.add(mdmModuleDbEntity);
            //新增 设置日志表
            setMdmField(mdmModuleDbEntity, mdmModuleDbFiledsEntities);

            MdmModuleDbEntity mdmModuleDbEntity1 = new MdmModuleDbEntity();
            mdmModuleDbEntity1.setMdmId(entity.getMdmId());
            mdmModuleDbEntity1.setDbName(entity.getDbName() + "_send_log");
            mdmModuleDbEntity1.setDbType("4");
            mdmModuleDbEntity1.setRemark(entity.getRemark() + "下发日志");
            mdmModuleDbEntity1.setCreate();
            mdmModuleDbEntity1.setDataType("1");
            mdmModuleDbEntities.add(mdmModuleDbEntity1);
            //新增 设置日志表
            setMdmField(mdmModuleDbEntity1, mdmModuleDbFiledsEntities);
        }
        for (int i = 0; i < mdmModuleDbEntities.size(); i++) {
            //查看表是否存在
            MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
            mdmModuleDbEntity.setDbName(mdmModuleDbEntities.get(i).getDbName());
            mdmModuleDbEntity.setSts("Y");
            int a = mdmModuleDbDao.getCount(mdmModuleDbEntity);
            if (a > 0) {
                return BaseResult.getFailureMessageEntity("表已存在，请修改");
            }
        }
        mdmServiceCache.saveOrUpdateMdmModuleDb(entity.getMdmId(), mdmModuleDbEntities);
        mdmServiceCache.saveOrUpdateMdmModuleDbFileds(entity.getMdmId(), mdmModuleDbFiledsEntities);

        //新建表
        for (int i = 0; i < mdmModuleDbEntities.size(); i++) {
            Map<String, Object> maps = new HashMap<>();
            maps.put("tableName", mdmModuleDbEntities.get(i).getDbName());//表名
            maps.put("tableRemark", mdmModuleDbEntities.get(i).getRemark());//表备注
            maps.put("tableType", mdmModuleDbEntities.get(i).getDbType());//表备注
            mdmModuleDao.createTable(maps);
        }
        return BaseResult.getSuccessMessageEntity("保存表成功");
    }

    /**
     * @param entity
     * @param mdmModuleDbFiledsEntities
     * @return void
     * @Author lvleigang
     * @Description 设置默认字段
     * @Date 10:47 上午 2024/6/12
     **/
    private void setMdmField(MdmModuleDbEntity entity, List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntities) {

        MdmModuleDbFiledsEntity id = new MdmModuleDbFiledsEntity();
        id.setMdmId(entity.getMdmId());
        id.setDbId(entity.getId());
        id.setChName("id");
        id.setEnName("id");
        id.setFiledType("3");
        id.setAddType("2");
        id.setUpdateType("2");
        id.setShowType("2");
        id.setQueryType("2");
        id.setListType("2");
        id.setViewType("2");
        id.setFiledLength("50");
        id.setCreate();
        id.setDataType("1");
        mdmModuleDbFiledsEntities.add(id);

        if ("1".equals(entity.getDbType())) {
            MdmModuleDbFiledsEntity documentRule = new MdmModuleDbFiledsEntity();
            documentRule.setMdmId(entity.getMdmId());
            documentRule.setDbId(entity.getId());
            documentRule.setEnName("document_rule");
            documentRule.setChName("单据规则");
            documentRule.setFiledType("3");
            documentRule.setAddType("2");
            documentRule.setUpdateType("1");
            documentRule.setShowType("1");
            documentRule.setQueryType("1");
            documentRule.setListType("1");
            documentRule.setViewType("2");
            documentRule.setFiledLength("50");
            documentRule.setCreate();
            documentRule.setDataType("1");

            mdmModuleDbFiledsEntities.add(documentRule);

            MdmModuleDbFiledsEntity document_rule_num = new MdmModuleDbFiledsEntity();
            document_rule_num.setMdmId(entity.getMdmId());
            document_rule_num.setDbId(entity.getId());
            document_rule_num.setEnName("document_rule_num");
            document_rule_num.setChName("单据规则流水号");
            document_rule_num.setFiledType("1");
            document_rule_num.setAddType("2");
            document_rule_num.setUpdateType("2");
            document_rule_num.setShowType("2");
            document_rule_num.setQueryType("2");
            document_rule_num.setListType("2");
            document_rule_num.setViewType("2");
            document_rule_num.setFiledLength("50");
            document_rule_num.setCreate();
            document_rule_num.setDataType("1");
            mdmModuleDbFiledsEntities.add(document_rule_num);

            MdmModuleDbFiledsEntity data_id = new MdmModuleDbFiledsEntity();
            data_id.setMdmId(entity.getMdmId());
            data_id.setDbId(entity.getId());
            data_id.setEnName("data_id");
            data_id.setChName("第三方主键ID");
            data_id.setFiledType("1");
            data_id.setAddType("2");
            data_id.setUpdateType("2");
            data_id.setShowType("2");
            data_id.setQueryType("2");
            data_id.setListType("2");
            data_id.setViewType("2");
            data_id.setFiledLength("255");
            data_id.setCreate();
            data_id.setDataType("1");
            mdmModuleDbFiledsEntities.add(data_id);

        }
        if ("2".equals(entity.getDbType()) || "3".equals(entity.getDbType()) || "4".equals(entity.getDbType())) {
            MdmModuleDbFiledsEntity formmain_id = new MdmModuleDbFiledsEntity();
            formmain_id.setMdmId(entity.getMdmId());
            formmain_id.setDbId(entity.getId());
            formmain_id.setEnName("formmain_id");
            formmain_id.setChName("主表id");
            formmain_id.setFiledType("3");
            formmain_id.setAddType("2");
            formmain_id.setUpdateType("2");
            formmain_id.setShowType("2");
            formmain_id.setQueryType("2");
            formmain_id.setListType("2");
            formmain_id.setViewType("2");
            formmain_id.setFiledLength("50");
            formmain_id.setCreate();
            formmain_id.setDataType("1");
            mdmModuleDbFiledsEntities.add(formmain_id);

            MdmModuleDbFiledsEntity data_id = new MdmModuleDbFiledsEntity();
            data_id.setMdmId(entity.getMdmId());
            data_id.setDbId(entity.getId());
            data_id.setEnName("data_id");
            data_id.setChName("第三方主键ID");
            data_id.setFiledType("1");
            data_id.setAddType("2");
            data_id.setUpdateType("2");
            data_id.setShowType("2");
            data_id.setQueryType("2");
            data_id.setListType("2");
            data_id.setViewType("2");
            data_id.setFiledLength("255");
            data_id.setCreate();
            data_id.setDataType("1");
            mdmModuleDbFiledsEntities.add(data_id);

        }
        //写入日志字段：操作人
        if ("3".equals(entity.getDbType())) {
            MdmModuleDbFiledsEntity source_name = new MdmModuleDbFiledsEntity();
            source_name.setMdmId(entity.getMdmId());
            source_name.setDbId(entity.getId());
            source_name.setEnName("source_name");
            source_name.setChName("来源名称");
            source_name.setFiledType("3");
            source_name.setAddType("2");
            source_name.setUpdateType("2");
            source_name.setShowType("2");
            source_name.setQueryType("2");
            source_name.setListType("2");
            source_name.setViewType("2");
            source_name.setFiledLength("255");
            source_name.setCreate();
            source_name.setDataType("1");
            mdmModuleDbFiledsEntities.add(source_name);

            MdmModuleDbFiledsEntity data_type = new MdmModuleDbFiledsEntity();
            data_type.setMdmId(entity.getMdmId());
            data_type.setDbId(entity.getId());
            data_type.setEnName("data_type");
            data_type.setChName("状态");
            data_type.setFiledType("3");
            data_type.setAddType("1");
            data_type.setUpdateType("1");
            data_type.setShowType("1");
            data_type.setQueryType("1");
            data_type.setListType("1");
            data_type.setViewType("1");
            data_type.setFiledLength("50");
            data_type.setCreate();
            data_type.setDataType("1");
            mdmModuleDbFiledsEntities.add(data_type);

            MdmModuleDbFiledsEntity remark = new MdmModuleDbFiledsEntity();
            remark.setMdmId(entity.getMdmId());
            remark.setDbId(entity.getId());
            remark.setEnName("remark");
            remark.setChName("备注");
            remark.setFiledType("3");
            remark.setAddType("1");
            remark.setUpdateType("1");
            remark.setShowType("1");
            remark.setQueryType("1");
            remark.setListType("1");
            remark.setViewType("1");
            remark.setFiledLength("255");
            remark.setCreate();
            remark.setDataType("1");
            mdmModuleDbFiledsEntities.add(remark);

            MdmModuleDbFiledsEntity code = new MdmModuleDbFiledsEntity();
            code.setMdmId(entity.getMdmId());
            code.setDbId(entity.getId());
            code.setEnName("code");
            code.setChName("编码");
            code.setFiledType("3");
            code.setAddType("1");
            code.setUpdateType("1");
            code.setShowType("1");
            code.setQueryType("1");
            code.setListType("1");
            code.setViewType("1");
            code.setFiledLength("255");
            code.setCreate();
            code.setDataType("1");
            mdmModuleDbFiledsEntities.add(code);

            MdmModuleDbFiledsEntity source_data = new MdmModuleDbFiledsEntity();
            source_data.setMdmId(entity.getMdmId());
            source_data.setDbId(entity.getId());
            source_data.setEnName("source_data");
            source_data.setChName("源数据");
            source_data.setFiledType("3");
            source_data.setAddType("1");
            source_data.setUpdateType("1");
            source_data.setShowType("1");
            source_data.setQueryType("1");
            source_data.setListType("1");
            source_data.setViewType("1");
            source_data.setFiledLength("255");
            source_data.setCreate();
            source_data.setDataType("1");
            mdmModuleDbFiledsEntities.add(source_data);

            MdmModuleDbFiledsEntity option_type = new MdmModuleDbFiledsEntity();
            option_type.setMdmId(entity.getMdmId());
            option_type.setDbId(entity.getId());
            option_type.setEnName("option_type");
            option_type.setChName("操作类型");
            option_type.setFiledType("3");
            option_type.setAddType("1");
            option_type.setUpdateType("1");
            option_type.setShowType("1");
            option_type.setQueryType("1");
            option_type.setListType("1");
            option_type.setViewType("1");
            option_type.setFiledLength("255");
            option_type.setCreate();
            option_type.setDataType("1");
            mdmModuleDbFiledsEntities.add(option_type);

            MdmModuleDbFiledsEntity option_Name = new MdmModuleDbFiledsEntity();
            option_Name.setMdmId(entity.getMdmId());
            option_Name.setDbId(entity.getId());
            option_Name.setEnName("option_name");
            option_Name.setChName("操作人");
            option_Name.setFiledType("3");
            option_Name.setAddType("1");
            option_Name.setUpdateType("1");
            option_Name.setShowType("1");
            option_Name.setQueryType("1");
            option_Name.setListType("1");
            option_Name.setViewType("1");
            option_Name.setFiledLength("255");
            option_Name.setCreate();
            option_Name.setDataType("1");
            mdmModuleDbFiledsEntities.add(option_Name);
        }

        //目标应用、目标api、
        if ("4".equals(entity.getDbType())) {
            MdmModuleDbFiledsEntity target_app = new MdmModuleDbFiledsEntity();
            target_app.setMdmId(entity.getMdmId());
            target_app.setDbId(entity.getId());
            target_app.setEnName("target_app");
            target_app.setChName("目标应用");
            target_app.setFiledType("3");
            target_app.setAddType("1");
            target_app.setUpdateType("1");
            target_app.setShowType("1");
            target_app.setQueryType("1");
            target_app.setListType("1");
            target_app.setViewType("1");
            target_app.setFiledLength("255");
            target_app.setCreate();
            target_app.setDataType("1");
            mdmModuleDbFiledsEntities.add(target_app);

            MdmModuleDbFiledsEntity target_api = new MdmModuleDbFiledsEntity();
            target_api.setMdmId(entity.getMdmId());
            target_api.setDbId(entity.getId());
            target_api.setEnName("target_api");
            target_api.setChName("目标api");
            target_api.setFiledType("3");
            target_api.setAddType("1");
            target_api.setUpdateType("1");
            target_api.setShowType("1");
            target_api.setQueryType("1");
            target_api.setListType("1");
            target_api.setViewType("1");
            target_api.setFiledLength("255");
            target_api.setCreate();
            target_api.setDataType("1");
            mdmModuleDbFiledsEntities.add(target_api);

            MdmModuleDbFiledsEntity source_data = new MdmModuleDbFiledsEntity();
            source_data.setMdmId(entity.getMdmId());
            source_data.setDbId(entity.getId());
            source_data.setEnName("source_data");
            source_data.setChName("源数据");
            source_data.setFiledType("3");
            source_data.setAddType("1");
            source_data.setUpdateType("1");
            source_data.setShowType("1");
            source_data.setQueryType("1");
            source_data.setListType("1");
            source_data.setViewType("1");
            source_data.setFiledLength("255");
            source_data.setCreate();
            source_data.setDataType("1");
            mdmModuleDbFiledsEntities.add(source_data);

            MdmModuleDbFiledsEntity option_type = new MdmModuleDbFiledsEntity();
            option_type.setMdmId(entity.getMdmId());
            option_type.setDbId(entity.getId());
            option_type.setEnName("option_type");
            option_type.setChName("操作类型");
            option_type.setFiledType("3");
            option_type.setAddType("1");
            option_type.setUpdateType("1");
            option_type.setShowType("1");
            option_type.setQueryType("1");
            option_type.setListType("1");
            option_type.setViewType("1");
            option_type.setFiledLength("255");
            option_type.setCreate();
            option_type.setDataType("1");
            mdmModuleDbFiledsEntities.add(option_type);

            MdmModuleDbFiledsEntity data_type = new MdmModuleDbFiledsEntity();
            data_type.setMdmId(entity.getMdmId());
            data_type.setDbId(entity.getId());
            data_type.setEnName("data_type");
            data_type.setChName("状态");
            data_type.setFiledType("3");
            data_type.setAddType("1");
            data_type.setUpdateType("1");
            data_type.setShowType("1");
            data_type.setQueryType("1");
            data_type.setListType("1");
            data_type.setViewType("1");
            data_type.setFiledLength("255");
            data_type.setCreate();
            data_type.setDataType("1");
            mdmModuleDbFiledsEntities.add(data_type);

            MdmModuleDbFiledsEntity remark = new MdmModuleDbFiledsEntity();
            remark.setMdmId(entity.getMdmId());
            remark.setDbId(entity.getId());
            remark.setEnName("remark");
            remark.setChName("备注");
            remark.setFiledType("3");
            remark.setAddType("1");
            remark.setUpdateType("1");
            remark.setShowType("1");
            remark.setQueryType("1");
            remark.setListType("1");
            remark.setViewType("1");
            remark.setFiledLength("255");
            remark.setCreate();
            remark.setDataType("1");
            mdmModuleDbFiledsEntities.add(remark);

            MdmModuleDbFiledsEntity distribute_id = new MdmModuleDbFiledsEntity();
            distribute_id.setMdmId(entity.getMdmId());
            distribute_id.setDbId(entity.getId());
            distribute_id.setEnName("distribute_id");
            distribute_id.setChName("下发表id");
            distribute_id.setFiledType("3");
            distribute_id.setAddType("1");
            distribute_id.setUpdateType("1");
            distribute_id.setShowType("1");
            distribute_id.setQueryType("1");
            distribute_id.setListType("1");
            distribute_id.setViewType("1");
            distribute_id.setFiledLength("50");
            distribute_id.setCreate();
            distribute_id.setDataType("1");
            mdmModuleDbFiledsEntities.add(distribute_id);

            MdmModuleDbFiledsEntity app_id = new MdmModuleDbFiledsEntity();
            app_id.setMdmId(entity.getMdmId());
            app_id.setDbId(entity.getId());
            app_id.setEnName("app_id");
            app_id.setChName("app_id");
            app_id.setFiledType("3");
            app_id.setAddType("1");
            app_id.setUpdateType("1");
            app_id.setShowType("1");
            app_id.setQueryType("1");
            app_id.setListType("1");
            app_id.setViewType("1");
            app_id.setFiledLength("50");
            app_id.setCreate();
            app_id.setDataType("1");
            mdmModuleDbFiledsEntities.add(app_id);

            MdmModuleDbFiledsEntity api_id = new MdmModuleDbFiledsEntity();
            api_id.setMdmId(entity.getMdmId());
            api_id.setDbId(entity.getId());
            api_id.setEnName("api_id");
            api_id.setChName("api_id");
            api_id.setFiledType("3");
            api_id.setAddType("1");
            api_id.setUpdateType("1");
            api_id.setShowType("1");
            api_id.setQueryType("1");
            api_id.setListType("1");
            api_id.setViewType("1");
            api_id.setFiledLength("50");
            api_id.setCreate();
            api_id.setDataType("1");
            mdmModuleDbFiledsEntities.add(api_id);
        }
        if ("2".equals(entity.getDbType()) || "1".equals(entity.getDbType())) {
            MdmModuleDbFiledsEntity data_status = new MdmModuleDbFiledsEntity();
            data_status.setMdmId(entity.getMdmId());
            data_status.setDbId(entity.getId());
            data_status.setEnName("data_status");
            data_status.setChName("数据状态");
            data_status.setFiledType("3");
            data_status.setAddType("2");
            data_status.setUpdateType("2");
            data_status.setShowType("2");
            data_status.setQueryType("2");
            data_status.setListType("2");
            data_status.setViewType("2");
            data_status.setFiledLength("1");
            data_status.setCreate();
            data_status.setDataType("1");
            mdmModuleDbFiledsEntities.add(data_status);
        }
        if ("1".equals(entity.getDbType())) {
            MdmModuleDbFiledsEntity add_status = new MdmModuleDbFiledsEntity();
            add_status.setMdmId(entity.getMdmId());
            add_status.setDbId(entity.getId());
            add_status.setEnName("add_status");
            add_status.setChName("新增数据状态 0待下发 1已下发");
            add_status.setFiledType("3");
            add_status.setAddType("2");
            add_status.setUpdateType("2");
            add_status.setShowType("2");
            add_status.setQueryType("2");
            add_status.setListType("2");
            add_status.setViewType("2");
            add_status.setFiledLength("1");
            add_status.setCreate();
            add_status.setDataType("1");
            mdmModuleDbFiledsEntities.add(add_status);
        }
        if ("1".equals(entity.getDbType())) {
            MdmModuleDbFiledsEntity update_status = new MdmModuleDbFiledsEntity();
            update_status.setMdmId(entity.getMdmId());
            update_status.setDbId(entity.getId());
            update_status.setEnName("update_status");
            update_status.setChName("修改数据状态 0待下发 1已下发");
            update_status.setFiledType("3");
            update_status.setAddType("2");
            update_status.setUpdateType("2");
            update_status.setShowType("2");
            update_status.setQueryType("2");
            update_status.setListType("2");
            update_status.setViewType("2");
            update_status.setFiledLength("1");
            update_status.setCreate();
            update_status.setDataType("1");
            mdmModuleDbFiledsEntities.add(update_status);
        }

        if ("1".equals(entity.getDbType())) {
            MdmModuleDbFiledsEntity delete_status = new MdmModuleDbFiledsEntity();
            delete_status.setMdmId(entity.getMdmId());
            delete_status.setDbId(entity.getId());
            delete_status.setEnName("delete_status");
            delete_status.setChName("删除数据状态 0待下发 1已下发");
            delete_status.setFiledType("3");
            delete_status.setAddType("2");
            delete_status.setUpdateType("2");
            delete_status.setShowType("2");
            delete_status.setQueryType("2");
            delete_status.setListType("2");
            delete_status.setViewType("2");
            delete_status.setFiledLength("1");
            delete_status.setCreate();
            delete_status.setDataType("1");
            mdmModuleDbFiledsEntities.add(delete_status);
        }

        MdmModuleDbFiledsEntity sorts = new MdmModuleDbFiledsEntity();
        sorts.setMdmId(entity.getMdmId());
        sorts.setDbId(entity.getId());
        sorts.setEnName("sorts");
        sorts.setChName("排序");
        sorts.setFiledType("1");
        sorts.setAddType("2");
        sorts.setUpdateType("2");
        sorts.setShowType("2");
        sorts.setQueryType("2");
        sorts.setListType("2");
        sorts.setViewType("2");
        sorts.setFiledLength("20");
        sorts.setCreate();
        sorts.setDataType("1");
        mdmModuleDbFiledsEntities.add(sorts);
        MdmModuleDbFiledsEntity create_user_id = new MdmModuleDbFiledsEntity();
        create_user_id.setMdmId(entity.getMdmId());
        create_user_id.setDbId(entity.getId());
        create_user_id.setEnName("create_user_id");
        create_user_id.setChName("创建人id");
        create_user_id.setFiledType("3");
        create_user_id.setAddType("2");
        create_user_id.setUpdateType("2");
        create_user_id.setShowType("2");
        create_user_id.setQueryType("2");
        create_user_id.setListType("2");
        create_user_id.setViewType("2");
        create_user_id.setFiledLength("50");
        create_user_id.setCreate();
        create_user_id.setDataType("1");
        mdmModuleDbFiledsEntities.add(create_user_id);
        MdmModuleDbFiledsEntity create_time = new MdmModuleDbFiledsEntity();
        create_time.setMdmId(entity.getMdmId());
        create_time.setDbId(entity.getId());
        create_time.setEnName("create_time");
        create_time.setChName("创建时间");
        create_time.setFiledType("4");
        create_time.setAddType("2");
        create_time.setUpdateType("2");
        create_time.setShowType("2");
        create_time.setQueryType("2");
        create_time.setListType("2");
        create_time.setViewType("2");
        create_time.setFiledLength(null);
        create_time.setCreate();
        create_time.setDataType("1");
        mdmModuleDbFiledsEntities.add(create_time);
        MdmModuleDbFiledsEntity modify_user_id = new MdmModuleDbFiledsEntity();
        modify_user_id.setMdmId(entity.getMdmId());
        modify_user_id.setDbId(entity.getId());
        modify_user_id.setEnName("modify_user_id");
        modify_user_id.setChName("修改人id");
        modify_user_id.setFiledType("3");
        modify_user_id.setAddType("2");
        modify_user_id.setUpdateType("2");
        modify_user_id.setShowType("2");
        modify_user_id.setQueryType("2");
        modify_user_id.setListType("2");
        modify_user_id.setViewType("2");
        modify_user_id.setFiledLength("50");
        modify_user_id.setCreate();
        modify_user_id.setDataType("1");
        mdmModuleDbFiledsEntities.add(modify_user_id);
        MdmModuleDbFiledsEntity modify_time = new MdmModuleDbFiledsEntity();
        modify_time.setMdmId(entity.getMdmId());
        modify_time.setDbId(entity.getId());
        modify_time.setEnName("modify_time");
        modify_time.setChName("修改时间");
        modify_time.setFiledType("4");
        modify_time.setAddType("2");
        modify_time.setUpdateType("2");
        modify_time.setShowType("2");
        modify_time.setQueryType("2");
        modify_time.setListType("2");
        modify_time.setViewType("2");
        modify_time.setFiledLength(null);
        modify_time.setCreate();
        modify_time.setDataType("1");
        mdmModuleDbFiledsEntities.add(modify_time);
        MdmModuleDbFiledsEntity sts = new MdmModuleDbFiledsEntity();
        sts.setMdmId(entity.getMdmId());
        sts.setDbId(entity.getId());
        sts.setEnName("sts");
        sts.setChName("状态(Y正常N删除)");
        sts.setFiledType("3");
        sts.setAddType("2");
        sts.setUpdateType("2");
        sts.setShowType("2");
        sts.setQueryType("2");
        sts.setListType("2");
        sts.setViewType("2");
        sts.setFiledLength("1");
        sts.setCreate();
        sts.setDataType("1");
        mdmModuleDbFiledsEntities.add(sts);
        MdmModuleDbFiledsEntity org_id = new MdmModuleDbFiledsEntity();
        org_id.setMdmId(entity.getMdmId());
        org_id.setDbId(entity.getId());
        org_id.setEnName("org_id");
        org_id.setChName("组织机构ID");
        org_id.setFiledType("3");
        org_id.setAddType("2");
        org_id.setUpdateType("2");
        org_id.setShowType("2");
        org_id.setQueryType("2");
        org_id.setListType("2");
        org_id.setViewType("2");
        org_id.setFiledLength("50");
        org_id.setCreate();
        org_id.setDataType("1");
        mdmModuleDbFiledsEntities.add(org_id);
        MdmModuleDbFiledsEntity company_id = new MdmModuleDbFiledsEntity();
        company_id.setMdmId(entity.getMdmId());
        company_id.setDbId(entity.getId());
        company_id.setEnName("company_id");
        company_id.setChName("公司id");
        company_id.setFiledType("3");
        company_id.setAddType("2");
        company_id.setUpdateType("2");
        company_id.setShowType("2");
        company_id.setQueryType("2");
        company_id.setListType("2");
        company_id.setViewType("2");
        company_id.setFiledLength("50");
        company_id.setCreate();
        company_id.setDataType("1");
        mdmModuleDbFiledsEntities.add(company_id);
    }


    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-数据源-数据表修改
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity updateMdmDb(JSONObject object) {
        MdmModuleDbEntity entity = getData("jsonStr", object, MdmModuleDbEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmId() == null || "".equals(entity.getMdmId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getDbName() == null || "".equals(entity.getDbName())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        List<MdmModuleDbEntity> mdmModuleDbEntities = new ArrayList<>();
        entity.setDataType("2");
        mdmModuleDbEntities.add(entity);
        mdmServiceCache.saveOrUpdateMdmModuleDb(entity.getMdmId(), mdmModuleDbEntities);
        Map<String, String> tablename = new HashMap<>();
        tablename.put("tableName", entity.getDbName());//表名
        tablename.put("tableRemark", entity.getRemark());//表备注
        mdmModuleDao.alterTableName(tablename);
        return BaseResult.getSuccessMessageEntity("修改数据源成功");
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-数据源-数据表删除
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity deleteMdmDb(JSONObject object) {
        MdmModuleDbEntity entity = getData("jsonStr", object, MdmModuleDbEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmId() == null || "".equals(entity.getMdmId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getDbName() == null || "".equals(entity.getDbName())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getDbType() == null || "".equals(entity.getDbType())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }

        MdmQuery mdmQuery = new MdmQuery();
        mdmQuery.setTableName(entity.getDbName());
        Integer count = mdmModuleDao.queryMainCount(mdmQuery);
        if (count > 0) {
            return BaseResult.getFailureMessageEntity(entity.getDbName() + "存在数据，不允许删除");
        }
        MdmModuleDbFiledsRuleEntity queryCount = new MdmModuleDbFiledsRuleEntity();
        queryCount.setMdmId(entity.getMdmId());
        queryCount.setRuleCode("service");
        queryCount.setRuleValue(entity.getDbName());
        Integer counts = mdmModuleDbFiledsRuleDao.queryUserMdm(queryCount);
        if (counts > 0) {
            return BaseResult.getFailureMessageEntity(entity.getDbName() + "已被引用，不允许删除");
        }

        if ("1".equals(entity.getDbType())) {
            //删除视图设置的字段
            MdmModuleViewDetailEntity mdmModuleViewDetailEntity = new MdmModuleViewDetailEntity();
            mdmModuleViewDetailEntity.setUpdate();
            mdmModuleViewDetailEntity.setMdmId(entity.getMdmId());
            mdmModuleViewDetailDao.logicRemoveMultiCondition(mdmModuleViewDetailEntity);

            //删除显示信息
            MdmModuleViewEntity mdmModuleViewEntity = new MdmModuleViewEntity();
            mdmModuleViewEntity.setUpdate();
            mdmModuleViewEntity.setMdmId(entity.getMdmId());
            mdmModuleViewDao.logicRemoveMultiCondition(mdmModuleViewEntity);

            //删除字段规则表
            MdmModuleDbFiledsRuleEntity mdmModuleDbFiledsRuleEntity = new MdmModuleDbFiledsRuleEntity();
            mdmModuleDbFiledsRuleEntity.setUpdate();
            mdmModuleDbFiledsRuleEntity.setMdmId(entity.getMdmId());
            mdmModuleDbFiledsRuleDao.logicRemoveMultiCondition(mdmModuleDbFiledsRuleEntity);
            //删除字段表
            mdmServiceCache.deleteMdmModuleDbFileds(entity.getMdmId(), null);
            //删除设置的db，以及实体表
            MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
            mdmModuleDbEntity.setMdmId(entity.getMdmId());
            List<MdmModuleDbEntity> mdmModuleDbEntities1 = mdmServiceCache.queryMdmModuleDb(mdmModuleDbEntity);
            if (mdmModuleDbEntities1 != null && mdmModuleDbEntities1.size() > 0) {
                for (int i = 0; i < mdmModuleDbEntities1.size(); i++) {
                    Map<String, Object> maps = new HashMap<>();
                    maps.put("tableName", mdmModuleDbEntities1.get(i).getDbName());
                    mdmModuleDao.deleteTable(maps);
                }
            }
            mdmServiceCache.deleteMdmModuleDb(entity.getMdmId(), null);

            //删除菜单和按钮，以及权限分配（用户权限和角色权限）
            SysMenuConfigEntity sysMenuConfigEntity = new SysMenuConfigEntity();
            sysMenuConfigEntity.setFormId(entity.getMdmId());
            sysMenuConfigEntity.setSts("Y");
            List<SysMenuConfigEntity> sysMenuConfigEntities = sysMenuConfigDao.queryBase(sysMenuConfigEntity);
            if (sysMenuConfigEntities != null && sysMenuConfigEntities.size() > 0) {
                for (int i = 0; i < sysMenuConfigEntities.size(); i++) {
                    SysButtonConfigEntity sysButtonConfigEntity = new SysButtonConfigEntity();
                    sysButtonConfigEntity.setMenuId(sysMenuConfigEntities.get(i).getId());
                    sysButtonConfigEntity.setSts("Y");
                    sysButtonConfigEntity.setUpdate();
                    sysButtonConfigDao.logicRemoveMultiCondition(sysButtonConfigEntity);

                    SysPopedomOperateEntity sysPopedomOperateEntity = new SysPopedomOperateEntity();
                    sysPopedomOperateEntity.setMenuId(sysMenuConfigEntities.get(i).getId());
                    sysPopedomOperateEntity.setSts("Y");
                    sysPopedomOperateEntity.setUpdate();
                    sysPopedomOperateDao.logicRemoveMultiCondition(sysPopedomOperateEntity);

                    SysPopedomMenuEntity sysPopedomMenuEntity = new SysPopedomMenuEntity();
                    sysPopedomMenuEntity.setMenuId(sysMenuConfigEntities.get(i).getId());
                    sysPopedomMenuEntity.setSts("Y");
                    sysPopedomMenuEntity.setUpdate();
                    sysPopedomMenuDao.logicRemoveMultiCondition(sysPopedomMenuEntity);
                    sysMenuConfigEntities.get(i).setUpdate();
                    sysMenuConfigDao.logicRemoveMultiCondition(sysMenuConfigEntities.get(i));
                }
            }
        }

        if ("2".equals(entity.getDbType())) {
            MdmModuleDbFiledsEntity mdmModuleDbFiledsEntity = new MdmModuleDbFiledsEntity();
            mdmModuleDbFiledsEntity.setDbId(entity.getId());
            List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntities = mdmModuleDbFiledsDao.queryBase(mdmModuleDbFiledsEntity);
            if (mdmModuleDbFiledsEntities != null && mdmModuleDbFiledsEntities.size() > 0) {
                for (int i = 0; i < mdmModuleDbFiledsEntities.size(); i++) {
                    //删除视图设置的字段
                    MdmModuleViewDetailEntity mdmModuleViewDetailEntity = new MdmModuleViewDetailEntity();
                    mdmModuleViewDetailEntity.setUpdate();
                    mdmModuleViewDetailEntity.setMdmId(entity.getMdmId());
                    mdmModuleViewDetailEntity.setViewFiled(mdmModuleDbFiledsEntities.get(i).getId());
                    mdmModuleViewDetailDao.logicRemoveMultiCondition(mdmModuleViewDetailEntity);
                }
            }
            //删除字段规则表
            MdmModuleDbFiledsRuleEntity mdmModuleDbFiledsRuleEntity = new MdmModuleDbFiledsRuleEntity();
            mdmModuleDbFiledsRuleEntity.setUpdate();
            mdmModuleDbFiledsRuleEntity.setMdmId(entity.getMdmId());
            mdmModuleDbFiledsRuleEntity.setDbId(entity.getId());
            mdmModuleDbFiledsRuleDao.logicRemoveMultiCondition(mdmModuleDbFiledsRuleEntity);
            //删除字段表
            mdmServiceCache.deleteMdmModuleDbFileds(entity.getMdmId(), entity.getId());
            //删除设置的db，以及实体表

            Map<String, Object> maps = new HashMap<>();
            maps.put("tableName", entity.getDbName());
            mdmModuleDao.deleteTable(maps);
            mdmServiceCache.deleteMdmModuleDb(entity.getMdmId(), entity.getId());
        }

        return BaseResult.getSuccessMessageEntity("删除成功");
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-数据源-数据表字段获取
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity queryMdmDbField(JSONObject object) {
        MdmModuleDbFiledsEntity entity = getData("jsonStr", object, MdmModuleDbFiledsEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmId() == null || "".equals(entity.getMdmId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getDbId() == null || "".equals(entity.getDbId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        MdmModuleDbFiledsEntity queryEntity = new MdmModuleDbFiledsEntity();
        queryEntity.setMdmId(entity.getMdmId());
        List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntities = mdmServiceCache.queryMdmModuleDbFileds(queryEntity);
        List<MdmModuleDbFiledsEntity> dbEntities = new ArrayList<>();
        if (mdmModuleDbFiledsEntities != null && mdmModuleDbFiledsEntities.size() > 0) {
            for (int i = 0; i < mdmModuleDbFiledsEntities.size(); i++) {
                if (entity.getDbId().equals(mdmModuleDbFiledsEntities.get(i).getDbId()) && "1".equals(mdmModuleDbFiledsEntities.get(i).getViewType())) {
                    dbEntities.add(mdmModuleDbFiledsEntities.get(i));
                    MdmModuleDbFiledsRuleEntity ruleEntity = new MdmModuleDbFiledsRuleEntity();
                    ruleEntity.setMdmId(mdmModuleDbFiledsEntities.get(i).getMdmId());
                    ruleEntity.setFiledId(mdmModuleDbFiledsEntities.get(i).getId());
                    List<MdmModuleDbFiledsRuleEntity> mdmModuleDbFiledsRuleEntities = mdmModuleDbFiledsRuleDao.queryBase(ruleEntity);
                    mdmModuleDbFiledsEntities.get(i).setMdmModuleDbFiledsRules(mdmModuleDbFiledsRuleEntities);
                }
            }
        }
        return BaseResult.getSuccessMessageEntity("获取数据源表字段成功", dbEntities);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-数据源-数据表字段明细
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity queryMdmDbFieldDetail(JSONObject object) {
        MdmModuleDbFiledsEntity entity = getData("jsonStr", object, MdmModuleDbFiledsEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmId() == null || "".equals(entity.getMdmId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getDbId() == null || "".equals(entity.getDbId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        MdmModuleDbFiledsEntity queryEntity = new MdmModuleDbFiledsEntity();
        queryEntity.setMdmId(entity.getMdmId());
        List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntities = mdmServiceCache.queryMdmModuleDbFileds(queryEntity);
        List<MdmModuleDbFiledsEntity> dbEntities = new ArrayList<>();
        if (mdmModuleDbFiledsEntities != null && mdmModuleDbFiledsEntities.size() > 0) {
            for (int i = 0; i < mdmModuleDbFiledsEntities.size(); i++) {
                if (entity.getId().equals(mdmModuleDbFiledsEntities.get(i).getId())) {
                    entity = mdmModuleDbFiledsEntities.get(i);
                    MdmModuleDbFiledsRuleEntity ruleEntity = new MdmModuleDbFiledsRuleEntity();
                    ruleEntity.setMdmId(entity.getMdmId());
                    ruleEntity.setFiledId(entity.getId());
                    List<MdmModuleDbFiledsRuleEntity> mdmModuleDbFiledsRuleEntities = mdmModuleDbFiledsRuleDao.queryBase(ruleEntity);
                    entity.setMdmModuleDbFiledsRules(mdmModuleDbFiledsRuleEntities);
                }
            }
        }
        return BaseResult.getSuccessMessageEntity("获取数据源表字段成功", entity);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-数据源-数据表字段新增
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity saveMdmDbField(JSONObject object) {
        MdmModuleDbFiledsEntity entity = getData("jsonStr", object, MdmModuleDbFiledsEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getDbName() == null || "".equals(entity.getDbName())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmId() == null || "".equals(entity.getMdmId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getDbId() == null || "".equals(entity.getDbId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getEnName() == null || "".equals(entity.getEnName())) {
            return BaseResult.getFailureMessageEntity("请输入中文名称");
        }
        if (entity.getChName() == null || "".equals(entity.getChName())) {
            return BaseResult.getFailureMessageEntity("请输入英文名称");
        }
        if (entity.getFiledType() == null || "".equals(entity.getChName())) {
            return BaseResult.getFailureMessageEntity("请选择字段类型");
        }
        if (entity.getDbType() == null || "".equals(entity.getDbType())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }

        MdmModuleDbFiledsEntity mdmModuleDbFiledsEntity = new MdmModuleDbFiledsEntity();
        mdmModuleDbFiledsEntity.setMdmId(entity.getMdmId());
        mdmModuleDbFiledsEntity.setSts("Y");
//        List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntityList = mdmServiceCache.queryMdmModuleDbFileds(mdmModuleDbFiledsEntity);
        List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntityList = mdmModuleDbFiledsDao.query(mdmModuleDbFiledsEntity);
        if (mdmModuleDbFiledsEntityList != null && mdmModuleDbFiledsEntityList.size() > 0) {
            for (int i = 0; i < mdmModuleDbFiledsEntityList.size(); i++) {
                if (entity.getDbId().equals(mdmModuleDbFiledsEntityList.get(i).getDbId()) && entity.getEnName().equals(mdmModuleDbFiledsEntityList.get(i).getEnName())) {
                    return BaseResult.getFailureMessageEntity("字段已存在，请修改");
                }
            }
        }
        //新增时：查询sysPk,sysCode,sysName段是否存在 add by zyd 2025-06-09
        boolean isSysPk = false;
        boolean isSysCode = false;
        boolean isSysName = false;
        boolean isSysDate = false;
        for (MdmModuleDbFiledsEntity dbFiledsEntity : mdmModuleDbFiledsEntityList) {
            if (entity.getDbId().equals(dbFiledsEntity.getDbId())) {
                if (entity.getDbId().equals(dbFiledsEntity.getDbId())) {
                    if ("Y".equals(dbFiledsEntity.getIsSysPk())) {
                        isSysPk = true;
                    }
                    if ("Y".equals(dbFiledsEntity.getIsSysCode())) {
                        isSysCode = true;
                    }
                    if ("Y".equals(dbFiledsEntity.getIsSysName())) {
                        isSysName = true;
                    }
                    if ("Y".equals(dbFiledsEntity.getIsSysDate())) {
                        isSysDate = true;
                    }
                }
            }
        }
        //判端该字段是不是sysPk,sysCode,sysName=true
        if ("Y".equals(entity.getIsSysPk()) && isSysPk) {
            return BaseResult.getFailureMessageEntity("系统主键字段已存在，请修改");
        }
        if ("Y".equals(entity.getIsSysCode()) && isSysCode) {
            return BaseResult.getFailureMessageEntity("系统code字段已存在，请修改");
        }
        if ("Y".equals(entity.getIsSysName()) && isSysName) {
            return BaseResult.getFailureMessageEntity("系统name字段已存在，请修改");
        }
        if ("Y".equals(entity.getIsSysDate()) && isSysDate) {
            return BaseResult.getFailureMessageEntity("系统date字段已存在，请修改");
        }
        entity.setCreate();
        if ("1".equals(entity.getDbType())) {
            entity.setAddType("1");
            entity.setUpdateType("1");
            entity.setShowType("1");
            entity.setQueryType("1");
            entity.setListType("1");
            entity.setViewType("1");
        } else {
            entity.setAddType("1");
            entity.setUpdateType("1");
            entity.setShowType("1");
            entity.setQueryType("1");
            entity.setListType("2");
            entity.setViewType("1");
        }
        entity.setDataType("1");
        List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntities = new ArrayList<>();
        mdmModuleDbFiledsEntities.add(entity);
        mdmServiceCache.saveOrUpdateMdmModuleDbFileds(entity.getMdmId(), mdmModuleDbFiledsEntities);

        if (entity.getMdmModuleDbFiledsRules() != null && entity.getMdmModuleDbFiledsRules().size() > 0) {
            for (int i = 0; i < entity.getMdmModuleDbFiledsRules().size(); i++) {
                entity.getMdmModuleDbFiledsRules().get(i).setDbId(entity.getDbId());
                entity.getMdmModuleDbFiledsRules().get(i).setMdmId(entity.getMdmId());
                entity.getMdmModuleDbFiledsRules().get(i).setFiledId(entity.getId());
                entity.getMdmModuleDbFiledsRules().get(i).setSts("Y");
                entity.getMdmModuleDbFiledsRules().get(i).setCreate();
                mdmModuleDbFiledsRuleDao.save(entity.getMdmModuleDbFiledsRules().get(i));
            }
        }

        mdmModuleDao.alterTableField(entity);
        return BaseResult.getSuccessMessageEntity("新增字段成功");
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-数据源-数据表字段修改
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity updateMdmDbField(JSONObject object) {
        MdmModuleDbFiledsEntity entity = getData("jsonStr", object, MdmModuleDbFiledsEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getDbName() == null || "".equals(entity.getDbName())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmId() == null || "".equals(entity.getMdmId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getDbId() == null || "".equals(entity.getDbId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getEnName() == null || "".equals(entity.getEnName())) {
            return BaseResult.getFailureMessageEntity("请输入中文名称");
        }
        if (entity.getChName() == null || "".equals(entity.getChName())) {
            return BaseResult.getFailureMessageEntity("请输入英文名称");
        }
        if (entity.getFiledType() == null || "".equals(entity.getChName())) {
            return BaseResult.getFailureMessageEntity("请选择字段类型");
        }
        entity.setUpdate();
        entity.setDataType("2");
        try {
            mdmModuleDao.alterTableField(entity);
        } catch (Exception e) {
            throw new BaseSystemException("表字段类型，修改失败，请联系管理员");
        }

        //更新时：查询sysPk,sysCode,sysName段是否存在 add by zyd 2025-06-09
        MdmModuleDbFiledsEntity mdmModuleDbFiledsEntity = new MdmModuleDbFiledsEntity();
        mdmModuleDbFiledsEntity.setDbId(entity.getDbId());
        mdmModuleDbFiledsEntity.setMdmId(entity.getMdmId());
        mdmModuleDbFiledsEntity.setSts("Y");
        List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntityList = mdmModuleDbFiledsDao.query(mdmModuleDbFiledsEntity);
        boolean isSysPk = false;
        boolean isSysCode = false;
        boolean isSysName = false;
        boolean isSysDate = false;
        for (MdmModuleDbFiledsEntity dbFiledsEntity : mdmModuleDbFiledsEntityList) {
            if (entity.getDbId().equals(dbFiledsEntity.getDbId())) {
                if (entity.getDbId().equals(dbFiledsEntity.getDbId())) {
                    if ("Y".equals(dbFiledsEntity.getIsSysPk())) {
                        isSysPk = true;
                    }
                    if ("Y".equals(dbFiledsEntity.getIsSysCode())) {
                        isSysCode = true;
                    }
                    if ("Y".equals(dbFiledsEntity.getIsSysName())) {
                        isSysName = true;
                    }
                    if ("Y".equals(dbFiledsEntity.getIsSysDate())) {
                        isSysDate = true;
                    }
                }
            }
        }
        //判端该字段是不是sysPk,sysCode,sysName=true
        if ("Y".equals(entity.getIsSysPk()) && isSysPk) {
            return BaseResult.getFailureMessageEntity("系统主键字段已存在，请修改");
        }
        if ("Y".equals(entity.getIsSysCode()) && isSysCode) {
            return BaseResult.getFailureMessageEntity("系统code字段已存在，请修改");
        }
        if ("Y".equals(entity.getIsSysName()) && isSysName) {
            return BaseResult.getFailureMessageEntity("系统name字段已存在，请修改");
        }
        if ("Y".equals(entity.getIsSysDate()) && isSysDate) {
            return BaseResult.getFailureMessageEntity("系统date字段已存在，请修改");
        }

        List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntities = new ArrayList<>();
        mdmModuleDbFiledsEntities.add(entity);
        mdmServiceCache.saveOrUpdateMdmModuleDbFileds(entity.getMdmId(), mdmModuleDbFiledsEntities);
        MdmModuleDbFiledsRuleEntity mdmModuleDbFiledsRuleEntity = new MdmModuleDbFiledsRuleEntity();
        mdmModuleDbFiledsRuleEntity.setFiledId(entity.getId());
        mdmModuleDbFiledsRuleEntity.setSts("Y");
        mdmModuleDbFiledsRuleEntity.setUpdate();
        mdmModuleDbFiledsRuleDao.logicRemoveMultiCondition(mdmModuleDbFiledsRuleEntity);
        if (entity.getMdmModuleDbFiledsRules() != null && entity.getMdmModuleDbFiledsRules().size() > 0) {
            for (int i = 0; i < entity.getMdmModuleDbFiledsRules().size(); i++) {
                entity.getMdmModuleDbFiledsRules().get(i).setDbId(entity.getDbId());
                entity.getMdmModuleDbFiledsRules().get(i).setMdmId(entity.getMdmId());
                entity.getMdmModuleDbFiledsRules().get(i).setFiledId(entity.getId());
                entity.getMdmModuleDbFiledsRules().get(i).setSts("Y");
                if (entity.getMdmModuleDbFiledsRules().get(i).getId() != null) {
                    entity.getMdmModuleDbFiledsRules().get(i).setUpdate();
                    mdmModuleDbFiledsRuleDao.update(entity.getMdmModuleDbFiledsRules().get(i));
                } else {
                    entity.getMdmModuleDbFiledsRules().get(i).setCreate();
                    mdmModuleDbFiledsRuleDao.save(entity.getMdmModuleDbFiledsRules().get(i));
                }
            }
        }
        //try {
        //    mdmModuleDao.alterTableField(entity);
        //} catch (Exception e){
        //    throw new BaseSystemException("表字段类型，修改失败，请联系管理员");
        //}
        return BaseResult.getSuccessMessageEntity("修改字段成功");
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-数据源-数据表字段删除
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity deleteMdmDbField(JSONObject object) {
        MdmModuleDbFiledsEntity entity = getData("jsonStr", object, MdmModuleDbFiledsEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmId() == null || "".equals(entity.getMdmId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getDbId() == null || "".equals(entity.getDbId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getDbName() == null || "".equals(entity.getDbName())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getEnName() == null || "".equals(entity.getEnName())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getFiledType() == null || "".equals(entity.getFiledType())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        MdmModuleDbFiledsRuleEntity queryCount = new MdmModuleDbFiledsRuleEntity();
        queryCount.setMdmId(entity.getMdmId());
        queryCount.setDbId(entity.getDbId());
        queryCount.setRuleCode("label");
        queryCount.setRuleValue(entity.getEnName());
        Integer counts = mdmModuleDbFiledsRuleDao.queryUserMdm(queryCount);
        if (counts > 0) {
            return BaseResult.getFailureMessageEntity(entity.getEnName() + "已被引用，不允许删除");
        }
        MdmModuleViewEntity mdmModuleViewEntity = new MdmModuleViewEntity();
        mdmModuleViewEntity.setMdmId(entity.getMdmId());
        mdmModuleViewEntity.setViewFiled(entity.getId());
        mdmModuleViewEntity.setUpIdFiled(entity.getId());
        Integer countss = mdmModuleViewDao.queryUserMdm(mdmModuleViewEntity);
        if (countss > 0) {
            return BaseResult.getFailureMessageEntity(entity.getEnName() + "已被显示引用，不允许删除");
        }
        entity.setUpdate();
        entity.setDataType("3");
        List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntities = new ArrayList<>();
        mdmModuleDbFiledsEntities.add(entity);
        mdmServiceCache.saveOrUpdateMdmModuleDbFileds(entity.getMdmId(), mdmModuleDbFiledsEntities);
        MdmModuleDbFiledsRuleEntity mdmModuleDbFiledsRuleEntity = new MdmModuleDbFiledsRuleEntity();
        mdmModuleDbFiledsRuleEntity.setFiledId(entity.getId());
        mdmModuleDbFiledsRuleEntity.setSts("Y");
        mdmModuleDbFiledsRuleEntity.setUpdate();
        mdmModuleDbFiledsRuleDao.logicRemoveMultiCondition(mdmModuleDbFiledsRuleEntity);
        MdmModuleViewDetailEntity mdmModuleViewDetailEntity = new MdmModuleViewDetailEntity();
        mdmModuleViewDetailEntity.setViewFiled(entity.getId());
        mdmModuleViewDetailEntity.setSts("Y");
        mdmModuleViewDetailEntity.setUpdate();
        mdmModuleViewDetailDao.logicRemoveMultiCondition(mdmModuleViewDetailEntity);
        mdmModuleDao.alterTableField(entity);
        return BaseResult.getSuccessMessageEntity("删除字段成功");
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-数据源-数据表字段规则获取
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity queryMdmDbFieldRule(JSONObject object) {
        MdmModuleDbFiledsRuleEntity entity = getData("jsonStr", object, MdmModuleDbFiledsRuleEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmId() == null || "".equals(entity.getMdmId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getDbId() == null || "".equals(entity.getDbId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getFiledId() == null || "".equals(entity.getFiledId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        List<MdmModuleDbFiledsRuleEntity> mdmModuleDbFiledsRuleEntities = mdmModuleDbFiledsRuleDao.queryBase(entity);
        return BaseResult.getSuccessMessageEntity("获取数据源表字段规则成功", mdmModuleDbFiledsRuleEntities);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-数据源-数据表字段规则修改
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity updateMdmDbFieldRule(JSONObject object) {

        MdmDto entity = getData("jsonStr", object, MdmDto.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getDbId() == null || "".equals(entity.getDbId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getFieldId() == null || "".equals(entity.getFieldId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmModuleDbFiledsRuleEntities() == null || entity.getMdmModuleDbFiledsRuleEntities().size() == 0) {
            return BaseResult.getFailureMessageEntity("请先设置字段规则");
        }

        MdmModuleDbFiledsRuleEntity mdmModuleDbFiledsRuleEntity = new MdmModuleDbFiledsRuleEntity();
        mdmModuleDbFiledsRuleEntity.setFiledId(entity.getFieldId());
        mdmModuleDbFiledsRuleEntity.setSts("Y");
        mdmModuleDbFiledsRuleEntity.setUpdate();
        mdmModuleDbFiledsRuleDao.logicRemoveMultiCondition(mdmModuleDbFiledsRuleEntity);
        if (entity.getMdmModuleDbFiledsRuleEntities() != null && entity.getMdmModuleDbFiledsRuleEntities().size() > 0) {
            for (int i = 0; i < entity.getMdmModuleDbFiledsRuleEntities().size(); i++) {
                entity.getMdmModuleDbFiledsRuleEntities().get(i).setDbId(entity.getDbId());
                entity.getMdmModuleDbFiledsRuleEntities().get(i).setMdmId(entity.getId());
                entity.getMdmModuleDbFiledsRuleEntities().get(i).setFiledId(entity.getFieldId());
                entity.getMdmModuleDbFiledsRuleEntities().get(i).setSts("Y");
                if (entity.getMdmModuleDbFiledsRuleEntities().get(i).getId() != null) {
                    entity.getMdmModuleDbFiledsRuleEntities().get(i).setUpdate();
                    mdmModuleDbFiledsRuleDao.update(entity.getMdmModuleDbFiledsRuleEntities().get(i));
                } else {
                    entity.getMdmModuleDbFiledsRuleEntities().get(i).setCreate();
                    mdmModuleDbFiledsRuleDao.save(entity.getMdmModuleDbFiledsRuleEntities().get(i));
                }
            }
        }
        return BaseResult.getSuccessMessageEntity("保存字段规则成功");
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-显示信息-显示类型获取
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity queryMdmView(JSONObject object) {
        MdmModuleViewEntity entity = getData("jsonStr", object, MdmModuleViewEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmId() == null || "".equals(entity.getMdmId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        List<MdmModuleViewEntity> moduleViewEntities = mdmModuleViewDao.queryBase(entity);

        if (moduleViewEntities == null || moduleViewEntities.size() == 0) {
            return BaseResult.getSuccessMessageEntity("获取显示类型成功");
        } else if (moduleViewEntities != null && moduleViewEntities.size() > 1) {
            return BaseResult.getFailureMessageEntity("获取显示类型失败");
        } else {
            return BaseResult.getSuccessMessageEntity("获取显示类型成功", moduleViewEntities.get(0));
        }
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-显示信息-显示类型保存
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity saveMdmView(JSONObject object) {
        MdmModuleViewEntity entity = getData("jsonStr", object, MdmModuleViewEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        MdmModuleEntity mdmModuleEntity = mdmModuleDao.get(entity.getMdmId());

        entity.setCreate();
        mdmModuleViewDao.save(entity);
        SysMenuConfigEntity module = new SysMenuConfigEntity();
        module.setCreate();
        module.setFormId(mdmModuleEntity.getId());
        if ("1".equals(mdmModuleEntity.getMdmType())) {
            //1、档案
            module.setParentMenuId("ea22757e99c144fb9ef381dd322e233c");
        } else {
            //2、单据
            module.setParentMenuId("67f82a6e7f664c5a89b7fc7b8fc817b0");
        }
        module.setMenuName(mdmModuleEntity.getMdmName());
        module.setMenuEnglishName("integrationOptionAdmin" + mdmModuleEntity.getMdmCode());
        module.setMenuIcon(null);//模块图标
        module.setRoute("integrationOptionAdmin/" + mdmModuleEntity.getMdmCode());//路由地址
        module.setMenuPage("integrationOptionV2/index");//一级Layout 二级 ParntView 三级component
        module.setRemark("主数据生成菜单");
        module.setVisibles("0");
        module.setShowType("1");
        JSONObject object1 = new JSONObject();
        object1.put("mdmId", mdmModuleEntity.getId());//主数据id
        object1.put("mdmCode", mdmModuleEntity.getMdmCode());//主数据编码
        object1.put("viewType", entity.getViewName());//1、树 2、列表
        module.setOptions(object1.toString());//菜单mate参数
        sysMenuConfigDao.save(module);
        SysButtonConfigEntity buttonConfigEntity = new SysButtonConfigEntity();
        buttonConfigEntity.setMenuId(module.getId());
        buttonConfigEntity.setSts("Y");
        List<SysButtonConfigEntity> sysButtonConfigEntities = sysButtonConfigDao.queryBase(buttonConfigEntity);
        //1、查询按钮权限
        SysPopedomOperateEntity sysPopedomOperateEntity = new SysPopedomOperateEntity();
        sysPopedomOperateEntity.setMenuId(module.getId());
        sysPopedomOperateEntity.setSts("Y");
        List<SysPopedomOperateEntity> sysPopedomOperateEntities = sysPopedomOperateDao.queryBase(sysPopedomOperateEntity);
        //删除按钮和删除按钮权限
        sysButtonConfigDao.logicRemoveMultiCondition(buttonConfigEntity);
        sysPopedomOperateDao.logicRemoveMultiCondition(sysPopedomOperateEntity);
        MdmModuleViewButtonEntity mdmModuleViewButtonEntity = new MdmModuleViewButtonEntity();
        mdmModuleViewButtonEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleViewButtonEntity.setSts("Y");
        List<MdmModuleViewButtonEntity> mdmModuleViewButtonEntities = mdmModuleViewButtonDao.queryBase(mdmModuleViewButtonEntity);

        //删除处理权限
        doMdmModuleButton(module, mdmModuleViewButtonEntities, sysButtonConfigEntities, sysPopedomOperateEntities);
        return BaseResult.getSuccessMessageEntity("保存显示类型成功");

    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-显示信息-显示类型修改
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity updateMdmView(JSONObject object) {
        MdmModuleViewEntity entity = getData("jsonStr", object, MdmModuleViewEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        MdmModuleEntity mdmModuleEntity = mdmModuleDao.get(entity.getMdmId());

        entity.setUpdate();
        mdmModuleViewDao.update(entity);
        SysMenuConfigEntity module = new SysMenuConfigEntity();
        module.setFormId(mdmModuleEntity.getId());
        List<SysMenuConfigEntity> sysMenuConfigEntities = sysMenuConfigDao.queryBase(module);

        if (sysMenuConfigEntities != null && sysMenuConfigEntities.size() == 1) {
            module = sysMenuConfigEntities.get(0);
            module.setUpdate();
            module.setFormId(mdmModuleEntity.getId());
            if ("1".equals(mdmModuleEntity.getMdmType())) {
                //1、档案
                module.setParentMenuId("ea22757e99c144fb9ef381dd322e233c");
            } else {
                //2、单据
                module.setParentMenuId("67f82a6e7f664c5a89b7fc7b8fc817b0");
            }
            module.setMenuName(mdmModuleEntity.getMdmName());
            module.setMenuEnglishName("integrationOptionAdmin" + mdmModuleEntity.getMdmCode());
            module.setMenuIcon(null);//模块图标
            module.setRoute("integrationOptionAdmin/" + mdmModuleEntity.getMdmCode());//路由地址
            module.setMenuPage("integrationOptionV2/index");//一级Layout 二级 ParntView 三级component
            module.setRemark("主数据生成菜单");
            module.setVisibles("0");
            module.setShowType("1");
            JSONObject object1 = new JSONObject();
            object1.put("mdmId", mdmModuleEntity.getId());//主数据id
            object1.put("mdmCode", mdmModuleEntity.getMdmCode());//主数据编码
            object1.put("viewType", entity.getViewName());//1、树 2、列表
            module.setOptions(object1.toString());//菜单mate参数
            sysMenuConfigDao.update(module);
            SysButtonConfigEntity buttonConfigEntity = new SysButtonConfigEntity();
            buttonConfigEntity.setMenuId(module.getId());
            buttonConfigEntity.setSts("Y");
            List<SysButtonConfigEntity> sysButtonConfigEntities = sysButtonConfigDao.queryBase(buttonConfigEntity);
            //1、查询按钮权限
            SysPopedomOperateEntity sysPopedomOperateEntity = new SysPopedomOperateEntity();
            sysPopedomOperateEntity.setMenuId(module.getId());
            sysPopedomOperateEntity.setSts("Y");
            List<SysPopedomOperateEntity> sysPopedomOperateEntities = sysPopedomOperateDao.queryBase(sysPopedomOperateEntity);
            //删除按钮和删除按钮权限
            sysButtonConfigDao.logicRemoveMultiCondition(buttonConfigEntity);
            sysPopedomOperateDao.logicRemoveMultiCondition(sysPopedomOperateEntity);
            MdmModuleViewButtonEntity mdmModuleViewButtonEntity = new MdmModuleViewButtonEntity();
            mdmModuleViewButtonEntity.setMdmId(mdmModuleEntity.getId());
            mdmModuleViewButtonEntity.setSts("Y");
            List<MdmModuleViewButtonEntity> mdmModuleViewButtonEntities = mdmModuleViewButtonDao.queryBase(mdmModuleViewButtonEntity);

            //删除处理权限
            doMdmModuleButton(module, mdmModuleViewButtonEntities, sysButtonConfigEntities, sysPopedomOperateEntities);
        }
        return BaseResult.getSuccessMessageEntity("修改显示类型成功");
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-显示信息-显示字段获取
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity queryMdmViewField(JSONObject object) {
        MdmDto entity = getData("jsonStr", object, MdmDto.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        MdmModuleViewVo mdmModuleViewVo = new MdmModuleViewVo();
        //查询设置各种类型字段
        //查询模版数据源
        MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
        mdmModuleDbEntity.setMdmId(entity.getId());
        mdmModuleDbEntity.setSts("Y");
        List<MdmModuleDbEntity> mdmModuleDbEntityList = mdmServiceCache.queryMdmModuleDb(mdmModuleDbEntity);
        if (mdmModuleDbEntityList != null && mdmModuleDbEntityList.size() > 0) {
            MdmModuleDbFiledsEntity mdmModuleDbFiledsEntity = new MdmModuleDbFiledsEntity();
            mdmModuleDbFiledsEntity.setMdmId(entity.getId());
            mdmModuleDbFiledsEntity.setSts("Y");
            List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntityList = mdmServiceCache.queryMdmModuleDbFileds(mdmModuleDbFiledsEntity);
            setFiledType(mdmModuleViewVo, mdmModuleDbEntityList, mdmModuleDbFiledsEntityList);
        }
        mdmModuleViewVo.setDbAddFiled(new ArrayList<>());
        mdmModuleViewVo.setDbEditFiled(new ArrayList<>());
        mdmModuleViewVo.setDbShowFiled(new ArrayList<>());
        mdmModuleViewVo.setDbQueryFiled(new ArrayList<>());
        mdmModuleViewVo.setDbListFiled(new ArrayList<>());

        MdmModuleViewDetailEntity mdmModuleViewDetailEntity = new MdmModuleViewDetailEntity();
        mdmModuleViewDetailEntity.setMdmId(entity.getId());
        mdmModuleViewDetailEntity.setSts("Y");
        List<MdmModuleViewDetailEntity> mdmModuleViewDetailEntityList = mdmModuleViewDetailDao.queryBase(mdmModuleViewDetailEntity);
        setDbFiledType(mdmModuleViewVo, mdmModuleViewDetailEntityList);


        return BaseResult.getSuccessMessageEntity("查询视图成功", mdmModuleViewVo);
    }

    private void setDbFiledType(MdmModuleViewVo mdmModuleViewVo, List<MdmModuleViewDetailEntity> mdmModuleViewDetailEntityList) {
        //新增
        List<MdmModuleViewDetailEntity> addFiled = new ArrayList<>();
        //修改
        List<MdmModuleViewDetailEntity> editFiled = new ArrayList<>();
        //查看
        List<MdmModuleViewDetailEntity> showFiled = new ArrayList<>();
        //查询
        List<MdmModuleViewDetailEntity> queryFiled = new ArrayList<>();
        //列表
        List<MdmModuleViewDetailEntity> listFiled = new ArrayList<>();
        if (mdmModuleViewDetailEntityList != null && mdmModuleViewDetailEntityList.size() > 0) {
            for (int i = 0; i < mdmModuleViewDetailEntityList.size(); i++) {
                switch (mdmModuleViewDetailEntityList.get(i).getViewType()) {
                    //1、查询2、列表3、新增4、修改 5、查看
                    case "1":
                        if (mdmModuleViewVo.getQueryFiled() != null && mdmModuleViewVo.getQueryFiled().size() > 0) {
                            for (int i1 = 0; i1 < mdmModuleViewVo.getQueryFiled().size(); i1++) {
                                if (mdmModuleViewVo.getQueryFiled().get(i1).getId().equals(mdmModuleViewDetailEntityList.get(i).getViewFiled())) {
                                    mdmModuleViewDetailEntityList.get(i).setViewName(mdmModuleViewVo.getQueryFiled().get(i1).getName());
                                    queryFiled.add(mdmModuleViewDetailEntityList.get(i));
                                }
                            }
                        }
                        break;
                    case "2":
                        if (mdmModuleViewVo.getListFiled() != null && mdmModuleViewVo.getListFiled().size() > 0) {
                            for (int i1 = 0; i1 < mdmModuleViewVo.getListFiled().size(); i1++) {
                                if (mdmModuleViewVo.getListFiled().get(i1).getId().equals(mdmModuleViewDetailEntityList.get(i).getViewFiled())) {
                                    mdmModuleViewDetailEntityList.get(i).setViewName(mdmModuleViewVo.getListFiled().get(i1).getName());
                                    listFiled.add(mdmModuleViewDetailEntityList.get(i));
                                }
                            }
                        }
                        break;
                    case "3":
                        if (mdmModuleViewVo.getAddFiled() != null && mdmModuleViewVo.getAddFiled().size() > 0) {
                            for (int i1 = 0; i1 < mdmModuleViewVo.getAddFiled().size(); i1++) {
                                if (mdmModuleViewVo.getAddFiled().get(i1).getId().equals(mdmModuleViewDetailEntityList.get(i).getViewFiled())) {
                                    mdmModuleViewDetailEntityList.get(i).setViewName(mdmModuleViewVo.getAddFiled().get(i1).getName());
                                    addFiled.add(mdmModuleViewDetailEntityList.get(i));
                                }
                            }
                        }
                        break;
                    case "4":
                        if (mdmModuleViewVo.getEditFiled() != null && mdmModuleViewVo.getEditFiled().size() > 0) {
                            for (int i1 = 0; i1 < mdmModuleViewVo.getEditFiled().size(); i1++) {
                                if (mdmModuleViewVo.getEditFiled().get(i1).getId().equals(mdmModuleViewDetailEntityList.get(i).getViewFiled())) {
                                    mdmModuleViewDetailEntityList.get(i).setViewName(mdmModuleViewVo.getEditFiled().get(i1).getName());
                                    editFiled.add(mdmModuleViewDetailEntityList.get(i));
                                }
                            }
                        }
                        break;
                    case "5":
                        if (mdmModuleViewVo.getShowFiled() != null && mdmModuleViewVo.getShowFiled().size() > 0) {
                            for (int i1 = 0; i1 < mdmModuleViewVo.getShowFiled().size(); i1++) {
                                if (mdmModuleViewVo.getShowFiled().get(i1).getId().equals(mdmModuleViewDetailEntityList.get(i).getViewFiled())) {
                                    mdmModuleViewDetailEntityList.get(i).setViewName(mdmModuleViewVo.getShowFiled().get(i1).getName());
                                    showFiled.add(mdmModuleViewDetailEntityList.get(i));
                                }
                            }
                        }
                        break;
                    default:
                }
            }
        }
        mdmModuleViewVo.setDbAddFiled(addFiled);
        mdmModuleViewVo.setDbEditFiled(editFiled);
        mdmModuleViewVo.setDbShowFiled(showFiled);
        mdmModuleViewVo.setDbQueryFiled(queryFiled);
        mdmModuleViewVo.setDbListFiled(listFiled);
    }

    /**
     * @param mdmModuleViewVo
     * @param mdmModuleDbEntityList
     * @param mdmModuleDbFiledsEntityList
     * @return void
     * @Author lvleigang
     * @Description 设置对应的字段类型
     * @Date 2023/10/25
     **/
    private void setFiledType(MdmModuleViewVo mdmModuleViewVo, List<MdmModuleDbEntity> mdmModuleDbEntityList, List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntityList) {
        //新增
        List<MdmDbFiledVo> addFiled = new ArrayList<>();
        //修改
        List<MdmDbFiledVo> editFiled = new ArrayList<>();
        //查看
        List<MdmDbFiledVo> showFiled = new ArrayList<>();
        //查询
        List<MdmDbFiledVo> queryFiled = new ArrayList<>();
        //列表
        List<MdmDbFiledVo> listFiled = new ArrayList<>();
        if (mdmModuleDbEntityList != null && mdmModuleDbEntityList.size() > 0) {
            for (int i = 0; i < mdmModuleDbEntityList.size(); i++) {
                if ("3".equals(mdmModuleDbEntityList.get(i).getDbType()) || "4".equals(mdmModuleDbEntityList.get(i).getDbType())) {
                    continue;
                }
                if (mdmModuleDbFiledsEntityList != null && mdmModuleDbFiledsEntityList.size() > 0) {

                    for (int i1 = 0; i1 < mdmModuleDbFiledsEntityList.size(); i1++) {
                        if (mdmModuleDbEntityList.get(i).getId().equals(mdmModuleDbFiledsEntityList.get(i1).getDbId())) {
                            if ("document_rule".equals(mdmModuleDbFiledsEntityList.get(i1).getEnName())) {
                                continue;
                            }
                            if ("1".equals(mdmModuleDbEntityList.get(i).getDbType())) {
                                //1、主表
                                if ("1".equals(mdmModuleDbFiledsEntityList.get(i1).getAddType())) {
                                    addFiled.add(new MdmDbFiledVo(mdmModuleDbFiledsEntityList.get(i1).getId(), "【主表:" + mdmModuleDbEntityList.get(i).getDbName() + "】" + mdmModuleDbFiledsEntityList.get(i1).getChName()));
                                }
                                if ("1".equals(mdmModuleDbFiledsEntityList.get(i1).getUpdateType())) {
                                    editFiled.add(new MdmDbFiledVo(mdmModuleDbFiledsEntityList.get(i1).getId(), "【主表:" + mdmModuleDbEntityList.get(i).getDbName() + "】" + mdmModuleDbFiledsEntityList.get(i1).getChName()));
                                }
                                if ("1".equals(mdmModuleDbFiledsEntityList.get(i1).getShowType())) {
                                    showFiled.add(new MdmDbFiledVo(mdmModuleDbFiledsEntityList.get(i1).getId(), "【主表:" + mdmModuleDbEntityList.get(i).getDbName() + "】" + mdmModuleDbFiledsEntityList.get(i1).getChName()));
                                }
                                if ("1".equals(mdmModuleDbFiledsEntityList.get(i1).getQueryType())) {
                                    queryFiled.add(new MdmDbFiledVo(mdmModuleDbFiledsEntityList.get(i1).getId(), "【主表:" + mdmModuleDbEntityList.get(i).getDbName() + "】" + mdmModuleDbFiledsEntityList.get(i1).getChName()));
                                }
                                if ("1".equals(mdmModuleDbFiledsEntityList.get(i1).getListType())) {
                                    listFiled.add(new MdmDbFiledVo(mdmModuleDbFiledsEntityList.get(i1).getId(), "【主表:" + mdmModuleDbEntityList.get(i).getDbName() + "】" + mdmModuleDbFiledsEntityList.get(i1).getChName()));
                                }
                            } else {
                                //2、明细
                                if ("1".equals(mdmModuleDbFiledsEntityList.get(i1).getQueryType())) {
                                    queryFiled.add(new MdmDbFiledVo(mdmModuleDbFiledsEntityList.get(i1).getId(), "【子表:" + mdmModuleDbEntityList.get(i).getDbName() + "】" + mdmModuleDbFiledsEntityList.get(i1).getChName()));
                                }
                                if ("1".equals(mdmModuleDbFiledsEntityList.get(i1).getAddType())) {
                                    addFiled.add(new MdmDbFiledVo(mdmModuleDbFiledsEntityList.get(i1).getId(), "【子表:" + mdmModuleDbEntityList.get(i).getDbName() + "】" + mdmModuleDbFiledsEntityList.get(i1).getChName()));
                                }
                                if ("1".equals(mdmModuleDbFiledsEntityList.get(i1).getUpdateType())) {
                                    editFiled.add(new MdmDbFiledVo(mdmModuleDbFiledsEntityList.get(i1).getId(), "【子表:" + mdmModuleDbEntityList.get(i).getDbName() + "】" + mdmModuleDbFiledsEntityList.get(i1).getChName()));
                                }
                                if ("1".equals(mdmModuleDbFiledsEntityList.get(i1).getShowType())) {
                                    showFiled.add(new MdmDbFiledVo(mdmModuleDbFiledsEntityList.get(i1).getId(), "【子表:" + mdmModuleDbEntityList.get(i).getDbName() + "】" + mdmModuleDbFiledsEntityList.get(i1).getChName()));
                                }
                            }
                        }
                    }
                }
            }
        }
        mdmModuleViewVo.setAddFiled(addFiled);
        mdmModuleViewVo.setEditFiled(editFiled);
        mdmModuleViewVo.setShowFiled(showFiled);
        mdmModuleViewVo.setQueryFiled(queryFiled);
        mdmModuleViewVo.setListFiled(listFiled);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-显示信息-显示字段设置保存
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity saveOrUpdateMdmViewField(JSONObject object) {
        MdmModuleViewVo entity = getData("jsonStr", object, MdmModuleViewVo.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getMdmId() == null || "".equals(entity.getMdmId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getViewType() == null || "".equals(entity.getViewType())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        //1、查询2、列表3、新增4、修改 5、查看
        if ("1".equals(entity.getViewType())) {
            MdmModuleViewDetailEntity mdmModuleViewDetailEntity = new MdmModuleViewDetailEntity();
            mdmModuleViewDetailEntity.setMdmId(entity.getMdmId());
            mdmModuleViewDetailEntity.setViewType(entity.getViewType());
            mdmModuleViewDetailEntity.setUpdate();
            mdmModuleViewDetailDao.logicRemoveMultiCondition(mdmModuleViewDetailEntity);
            if (entity.getDbQueryFiled() != null && entity.getDbQueryFiled().size() > 0) {
                for (int i = 0; i < entity.getDbQueryFiled().size(); i++) {
                    entity.getDbQueryFiled().get(i).setViewType(entity.getViewType());
                    entity.getDbQueryFiled().get(i).setMdmId(entity.getMdmId());
                    entity.getDbQueryFiled().get(i).setCreate();
                    mdmModuleViewDetailDao.save(entity.getDbQueryFiled().get(i));
                }
            }

        } else if ("2".equals(entity.getViewType())) {
            MdmModuleViewDetailEntity mdmModuleViewDetailEntity = new MdmModuleViewDetailEntity();
            mdmModuleViewDetailEntity.setMdmId(entity.getMdmId());
            mdmModuleViewDetailEntity.setViewType(entity.getViewType());
            mdmModuleViewDetailEntity.setUpdate();
            mdmModuleViewDetailDao.logicRemoveMultiCondition(mdmModuleViewDetailEntity);
            if (entity.getDbListFiled() != null && entity.getDbListFiled().size() > 0) {
                for (int i = 0; i < entity.getDbListFiled().size(); i++) {
                    entity.getDbListFiled().get(i).setMdmId(entity.getMdmId());
                    entity.getDbListFiled().get(i).setViewType(entity.getViewType());
                    entity.getDbListFiled().get(i).setCreate();
                    mdmModuleViewDetailDao.save(entity.getDbListFiled().get(i));
                }
            }

        } else if ("3".equals(entity.getViewType())) {
            MdmModuleViewDetailEntity mdmModuleViewDetailEntity = new MdmModuleViewDetailEntity();
            mdmModuleViewDetailEntity.setMdmId(entity.getMdmId());
            mdmModuleViewDetailEntity.setViewType(entity.getViewType());
            mdmModuleViewDetailEntity.setUpdate();
            mdmModuleViewDetailDao.logicRemoveMultiCondition(mdmModuleViewDetailEntity);
            if (entity.getDbAddFiled() != null && entity.getDbAddFiled().size() > 0) {
                for (int i = 0; i < entity.getDbAddFiled().size(); i++) {
                    entity.getDbAddFiled().get(i).setMdmId(entity.getMdmId());
                    entity.getDbAddFiled().get(i).setViewType(entity.getViewType());
                    entity.getDbAddFiled().get(i).setCreate();
                    mdmModuleViewDetailDao.save(entity.getDbAddFiled().get(i));
                }
            }

        } else if ("4".equals(entity.getViewType())) {
            MdmModuleViewDetailEntity mdmModuleViewDetailEntity = new MdmModuleViewDetailEntity();
            mdmModuleViewDetailEntity.setMdmId(entity.getMdmId());
            mdmModuleViewDetailEntity.setViewType(entity.getViewType());
            mdmModuleViewDetailEntity.setUpdate();
            mdmModuleViewDetailDao.logicRemoveMultiCondition(mdmModuleViewDetailEntity);
            if (entity.getDbEditFiled() != null && entity.getDbEditFiled().size() > 0) {
                for (int i = 0; i < entity.getDbEditFiled().size(); i++) {
                    entity.getDbEditFiled().get(i).setMdmId(entity.getMdmId());
                    entity.getDbEditFiled().get(i).setViewType(entity.getViewType());
                    entity.getDbEditFiled().get(i).setCreate();
                    mdmModuleViewDetailDao.save(entity.getDbEditFiled().get(i));
                }
            }

        } else if ("5".equals(entity.getViewType())) {
            MdmModuleViewDetailEntity mdmModuleViewDetailEntity = new MdmModuleViewDetailEntity();
            mdmModuleViewDetailEntity.setMdmId(entity.getMdmId());
            mdmModuleViewDetailEntity.setViewType(entity.getViewType());
            mdmModuleViewDetailEntity.setUpdate();
            mdmModuleViewDetailDao.logicRemoveMultiCondition(mdmModuleViewDetailEntity);
            if (entity.getDbShowFiled() != null && entity.getDbShowFiled().size() > 0) {
                for (int i = 0; i < entity.getDbShowFiled().size(); i++) {
                    entity.getDbShowFiled().get(i).setMdmId(entity.getMdmId());
                    entity.getDbShowFiled().get(i).setViewType(entity.getViewType());
                    entity.getDbShowFiled().get(i).setCreate();
                    mdmModuleViewDetailDao.save(entity.getDbShowFiled().get(i));
                }
            }

        }
        return BaseResult.getSuccessMessageEntity("修改成功");
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-显示信息-显示按钮获取
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity queryMdmViewButton(JSONObject object) {
        MdmModuleViewButtonEntity entity = getData("jsonStr", object, MdmModuleViewButtonEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmId() == null || "".equals(entity.getMdmId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        List<MdmModuleViewButtonEntity> moduleViewButtonEntities = mdmModuleViewButtonDao.queryBase(entity);
        return BaseResult.getSuccessMessageEntity("获取显示按钮成功", moduleViewButtonEntities);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-显示信息-显示按钮保存
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity saveOrUpdateMdmViewButton(JSONObject object) {
        MdmModuleViewVo entity = getData("jsonStr", object, MdmModuleViewVo.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getMdmId() == null || "".equals(entity.getMdmId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }

        MdmModuleViewButtonEntity mdmModuleViewButtonEntity = new MdmModuleViewButtonEntity();
        mdmModuleViewButtonEntity.setMdmId(entity.getMdmId());
        mdmModuleViewButtonEntity.setUpdate();
        mdmModuleViewButtonDao.logicRemoveMultiCondition(mdmModuleViewButtonEntity);
        if (entity.getDbButtonFiled() != null && entity.getDbButtonFiled().size() > 0) {
            for (int i = 0; i < entity.getDbButtonFiled().size(); i++) {
                entity.getDbButtonFiled().get(i).setMdmId(entity.getMdmId());
                if (entity.getDbButtonFiled().get(i).getId() != null) {
                    entity.getDbButtonFiled().get(i).setUpdate();
                    mdmModuleViewButtonDao.update(entity.getDbButtonFiled().get(i));
                } else {
                    entity.getDbButtonFiled().get(i).setCreate();
                    mdmModuleViewButtonDao.save(entity.getDbButtonFiled().get(i));
                }
            }
        }
        MdmModuleEntity mdmModuleEntity = mdmModuleDao.get(entity.getMdmId());
        SysMenuConfigEntity module = new SysMenuConfigEntity();
        module.setFormId(mdmModuleEntity.getId());
        List<SysMenuConfigEntity> sysMenuConfigEntities = sysMenuConfigDao.queryBase(module);
        if (sysMenuConfigEntities != null && sysMenuConfigEntities.size() == 1) {
            module = sysMenuConfigEntities.get(0);
            SysButtonConfigEntity buttonConfigEntity = new SysButtonConfigEntity();
            buttonConfigEntity.setMenuId(module.getId());
            buttonConfigEntity.setSts("Y");
            List<SysButtonConfigEntity> sysButtonConfigEntities = sysButtonConfigDao.queryBase(buttonConfigEntity);
            //1、查询按钮权限
            SysPopedomOperateEntity sysPopedomOperateEntity = new SysPopedomOperateEntity();
            sysPopedomOperateEntity.setMenuId(module.getId());
            sysPopedomOperateEntity.setSts("Y");
            List<SysPopedomOperateEntity> sysPopedomOperateEntities = sysPopedomOperateDao.queryBase(sysPopedomOperateEntity);
            //删除按钮和删除按钮权限
            sysButtonConfigDao.logicRemoveMultiCondition(buttonConfigEntity);
            sysPopedomOperateDao.logicRemoveMultiCondition(sysPopedomOperateEntity);
            //删除处理权限
            doMdmModuleButton(module, entity.getDbButtonFiled(), sysButtonConfigEntities, sysPopedomOperateEntities);
        }
        return BaseResult.getSuccessMessageEntity("修改显示按钮成功");
    }

    /**
     * @param module                      菜单
     * @param mdmModuleViewButtonEntities 主数据按钮
     * @param sysButtonConfigEntities     菜单按钮
     * @param sysPopedomOperateEntities   按钮权限
     * @return void
     * @Author lvleigang
     * @Description
     * @Date 3:29 下午 2024/6/4
     **/
    private void doMdmModuleButton(SysMenuConfigEntity module, List<MdmModuleViewButtonEntity> mdmModuleViewButtonEntities, List<SysButtonConfigEntity> sysButtonConfigEntities, List<SysPopedomOperateEntity> sysPopedomOperateEntities) {
        //循环设置的按钮，再循环菜单的按钮，如果有把原先的修改状态，如果没有新增
        if (mdmModuleViewButtonEntities != null && mdmModuleViewButtonEntities.size() > 0) {
            for (int i = 0; i < mdmModuleViewButtonEntities.size(); i++) {
                boolean flag = false;
                if (sysButtonConfigEntities != null && sysButtonConfigEntities.size() > 0) {
                    for (int i1 = 0; i1 < sysButtonConfigEntities.size(); i1++) {
                        if (mdmModuleViewButtonEntities.get(i).getButtonValue().equals(sysButtonConfigEntities.get(i1).getCode())) {
                            flag = true;
                            sysButtonConfigEntities.get(i1).setSts("Y");
                            sysButtonConfigEntities.get(i1).setUpdate();
                            sysButtonConfigEntities.get(i1).setSorts(i + 1L);
                            sysButtonConfigDao.update(sysButtonConfigEntities.get(i1));
                            if (sysPopedomOperateEntities != null && sysPopedomOperateEntities.size() > 0) {
                                for (int i2 = 0; i2 < sysPopedomOperateEntities.size(); i2++) {
                                    if (sysPopedomOperateEntities.get(i2).getOperate().equals(sysButtonConfigEntities.get(i1).getId())) {
                                        sysPopedomOperateEntities.get(i2).setSts("Y");
                                        sysPopedomOperateEntities.get(i2).setUpdate();
                                        sysPopedomOperateDao.update(sysPopedomOperateEntities.get(i2));
                                    }
                                }
                            }
                            break;
                        }
                    }
                }
                //原先没有需要新增
                if (!flag) {
                    if ("new".equals(mdmModuleViewButtonEntities.get(i).getButtonValue())) {
                        SysButtonConfigEntity xz = new SysButtonConfigEntity();
                        xz.setCode("new");
                        xz.setNameCh("新建");
                        xz.setNameEn("new");
                        xz.setMenuId(module.getId());
                        xz.setIconName("");
                        xz.setStyles("");
                        xz.setBtnFunction("new");
                        xz.setRemark("主数据新建按钮");
                        xz.setSorts(i + 1L);
                        xz.setCreate();
                        sysButtonConfigDao.save(xz);
                    }
                    if ("resize".equals(mdmModuleViewButtonEntities.get(i).getButtonValue())) {

                        SysButtonConfigEntity cz = new SysButtonConfigEntity();
                        cz.setCode("resize");
                        cz.setNameCh("重置");
                        cz.setNameEn("resize");
                        cz.setMenuId(module.getId());
                        cz.setIconName("");
                        cz.setStyles("");
                        cz.setBtnFunction("resize");
                        cz.setRemark("主数据重置按钮");
                        cz.setSorts(i + 1L);

                        cz.setCreate();
                        sysButtonConfigDao.save(cz);
                    }
                    if ("search".equals(mdmModuleViewButtonEntities.get(i).getButtonValue())) {

                        SysButtonConfigEntity cx = new SysButtonConfigEntity();
                        cx.setCode("search");
                        cx.setNameCh("查询");
                        cx.setNameEn("search");
                        cx.setMenuId(module.getId());
                        cx.setIconName("");
                        cx.setStyles("");
                        cx.setBtnFunction("search");
                        cx.setRemark("主数据查询按钮");
                        cx.setSorts(i + 1L);
                        cx.setCreate();
                        sysButtonConfigDao.save(cx);
                    }
                    if ("edit".equals(mdmModuleViewButtonEntities.get(i).getButtonValue())) {

                        SysButtonConfigEntity xg = new SysButtonConfigEntity();
                        xg.setCode("edit");
                        xg.setNameCh("修改");
                        xg.setNameEn("edit");
                        xg.setMenuId(module.getId());
                        xg.setIconName("");
                        xg.setStyles("");
                        xg.setBtnFunction("edit");
                        xg.setRemark("主数据修改按钮");
                        xg.setSorts(i + 1L);
                        xg.setCreate();
                        sysButtonConfigDao.save(xg);
                    }
                    if ("dele".equals(mdmModuleViewButtonEntities.get(i).getButtonValue())) {

                        SysButtonConfigEntity sc = new SysButtonConfigEntity();
                        sc.setCode("dele");
                        sc.setNameCh("删除");
                        sc.setNameEn("dele");
                        sc.setMenuId(module.getId());
                        sc.setIconName("");
                        sc.setStyles("");
                        sc.setBtnFunction("dele");
                        sc.setRemark("主数据删除按钮");
                        sc.setSorts(i + 1L);
                        sc.setCreate();
                        sysButtonConfigDao.save(sc);
                    }
                    if ("view".equals(mdmModuleViewButtonEntities.get(i).getButtonValue())) {

                        SysButtonConfigEntity ck = new SysButtonConfigEntity();
                        ck.setCode("view");
                        ck.setNameCh("查看");
                        ck.setNameEn("view");
                        ck.setMenuId(module.getId());
                        ck.setIconName("");
                        ck.setStyles("");
                        ck.setBtnFunction("view");
                        ck.setRemark("主数据查看按钮");
                        ck.setSorts(i + 1L);
                        ck.setCreate();
                        sysButtonConfigDao.save(ck);
                    }
                    if ("send".equals(mdmModuleViewButtonEntities.get(i).getButtonValue())) {
                        SysButtonConfigEntity xf = new SysButtonConfigEntity();
                        xf.setCode("send");
                        xf.setNameCh("下发");
                        xf.setNameEn("send");
                        xf.setMenuId(module.getId());
                        xf.setIconName("");
                        xf.setStyles("");
                        xf.setBtnFunction("send");
                        xf.setRemark("主数据下发按钮");
                        xf.setSorts(i + 1L);
                        xf.setCreate();
                        sysButtonConfigDao.save(xf);
                    }
                    if ("viewData".equals(mdmModuleViewButtonEntities.get(i).getButtonValue())) {
                        SysButtonConfigEntity xf = new SysButtonConfigEntity();
                        xf.setCode("viewData");
                        xf.setNameCh("查看数据格式");
                        xf.setNameEn("viewData");
                        xf.setMenuId(module.getId());
                        xf.setIconName("");
                        xf.setStyles("");
                        xf.setBtnFunction("viewData");
                        xf.setRemark("查看数据格式");
                        xf.setSorts(i + 1L);
                        xf.setCreate();
                        sysButtonConfigDao.save(xf);
                    }

                }
            }
        }
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-数据来源-列表获取
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity queryMdmSource(JSONObject object) {
        MdmModuleSourceEntity entity = getData("jsonStr", object, MdmModuleSourceEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmId() == null || "".equals(entity.getMdmId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        List<MdmModuleSourceEntity> list = mdmModuleSourceDao.queryBase(entity);
        return BaseResult.getSuccessMessageEntity("获取数据来源成功", list);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-数据来源保存
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity saveMdmSource(JSONObject object) {
        MdmModuleSourceEntity entity = getData("jsonStr", object, MdmModuleSourceEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmId() == null || "".equals(entity.getMdmId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity.setCreate();
        mdmModuleSourceDao.save(entity);
        return BaseResult.getSuccessMessageEntity("保存数据来源成功");
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-数据来源获取
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity getMdmSource(JSONObject object) {
        MdmModuleSourceEntity entity = getData("jsonStr", object, MdmModuleSourceEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity = mdmModuleSourceDao.get(entity.getId());
        return BaseResult.getSuccessMessageEntity("获取数据来源成功", entity);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-数据来源修改
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity updateMdmSource(JSONObject object) {
        MdmModuleSourceEntity entity = getData("jsonStr", object, MdmModuleSourceEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity.setUpdate();
        mdmModuleSourceDao.update(entity);
        return BaseResult.getSuccessMessageEntity("修改数据来源成功");
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-数据来源删除
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity deleteMdmSource(JSONObject object) {
        MdmModuleSourceEntity entity = getData("jsonStr", object, MdmModuleSourceEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity.setUpdate();
        mdmModuleSourceDao.logicRemoveMultiCondition(entity);
        return BaseResult.getSuccessMessageEntity("删除数据来源成功");
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-分发设置-列表获取
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity queryMdmDistribute(JSONObject object) {
        MdmModuleDistributeEntity entity = getData("jsonStr", object, MdmModuleDistributeEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmId() == null || "".equals(entity.getMdmId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        List<MdmModuleDistributeEntity> list = mdmModuleDistributeDao.queryBase(entity);
        if (list != null && list.size() > 0) {
            MdmModuleDistributeDetailEntity mdmModuleDistributeDetailEntity = new MdmModuleDistributeDetailEntity();
            mdmModuleDistributeDetailEntity.setMdmId(entity.getMdmId());
            mdmModuleDistributeDetailEntity.setSts("Y");
            List<MdmModuleDistributeDetailEntity> listDetail = mdmModuleDistributeDetailDao.queryBase(mdmModuleDistributeDetailEntity);
            if (listDetail != null && listDetail.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    List<MdmModuleDistributeDetailEntity> mdmModuleDistributeDetailEntities = new ArrayList<>();
                    for (int i1 = 0; i1 < listDetail.size(); i1++) {
                        if (listDetail.get(i1).getDistributeId().equals(list.get(i).getId())) {
                            mdmModuleDistributeDetailEntities.add(listDetail.get(i1));
                        }
                    }
                    list.get(i).setMdmModuleDistributeDetailEntities(mdmModuleDistributeDetailEntities);
                }
            }

        }
        return BaseResult.getSuccessMessageEntity("获取分发设置成功", list);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-分发设置保存
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity saveMdmDistribute(JSONObject object) {
        MdmModuleDistributeEntity entity = getData("jsonStr", object, MdmModuleDistributeEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmId() == null || "".equals(entity.getMdmId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity.setCreate();
        mdmModuleDistributeDao.save(entity);
        if (entity.getMdmModuleDistributeDetailEntities() != null && entity.getMdmModuleDistributeDetailEntities().size() > 0) {
            for (int i = 0; i < entity.getMdmModuleDistributeDetailEntities().size(); i++) {
                entity.getMdmModuleDistributeDetailEntities().get(i).setCreate();
                entity.getMdmModuleDistributeDetailEntities().get(i).setDistributeId(entity.getId());
                entity.getMdmModuleDistributeDetailEntities().get(i).setMdmId(entity.getMdmId());
                mdmModuleDistributeDetailDao.save(entity.getMdmModuleDistributeDetailEntities().get(i));
            }
        }
        return BaseResult.getSuccessMessageEntity("保存分发设置成功");
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-分发设置获取
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity getMdmDistribute(JSONObject object) {
        MdmModuleDistributeEntity entity = getData("jsonStr", object, MdmModuleDistributeEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity = mdmModuleDistributeDao.get(entity.getId());
        MdmModuleDistributeDetailEntity mdmModuleDistributeDetailEntity = new MdmModuleDistributeDetailEntity();
        mdmModuleDistributeDetailEntity.setDistributeId(entity.getId());
        List<MdmModuleDistributeDetailEntity> mdmModuleDistributeDetailEntities = mdmModuleDistributeDetailDao.queryBase(mdmModuleDistributeDetailEntity);
        entity.setMdmModuleDistributeDetailEntities(mdmModuleDistributeDetailEntities);
        return BaseResult.getSuccessMessageEntity("获取分发设置成功", entity);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-分发设置修改
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity updateMdmDistribute(JSONObject object) {
        MdmModuleDistributeEntity entity = getData("jsonStr", object, MdmModuleDistributeEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity.setUpdate();
        mdmModuleDistributeDao.update(entity);
        MdmModuleDistributeDetailEntity mdmModuleDistributeDetailEntity = new MdmModuleDistributeDetailEntity();
        mdmModuleDistributeDetailEntity.setDistributeId(entity.getId());
        mdmModuleDistributeDetailEntity.setUpdate();
        mdmModuleDistributeDetailDao.logicRemoveMultiCondition(mdmModuleDistributeDetailEntity);
        if (entity.getMdmModuleDistributeDetailEntities() != null && entity.getMdmModuleDistributeDetailEntities().size() > 0) {
            for (int i = 0; i < entity.getMdmModuleDistributeDetailEntities().size(); i++) {
                entity.getMdmModuleDistributeDetailEntities().get(i).setSts("Y");
                entity.getMdmModuleDistributeDetailEntities().get(i).setMdmId(entity.getMdmId());
                entity.getMdmModuleDistributeDetailEntities().get(i).setSts("Y");
                if (entity.getMdmModuleDistributeDetailEntities().get(i).getId() != null) {
                    entity.getMdmModuleDistributeDetailEntities().get(i).setUpdate();
                    entity.getMdmModuleDistributeDetailEntities().get(i).setDistributeId(entity.getId());
                    mdmModuleDistributeDetailDao.update(entity.getMdmModuleDistributeDetailEntities().get(i));
                } else {
                    entity.getMdmModuleDistributeDetailEntities().get(i).setCreate();
                    entity.getMdmModuleDistributeDetailEntities().get(i).setDistributeId(entity.getId());
                    mdmModuleDistributeDetailDao.save(entity.getMdmModuleDistributeDetailEntities().get(i));
                }
            }
        }
        return BaseResult.getSuccessMessageEntity("修改数据来源成功");
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-分发设置删除
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity deleteMdmDistribute(JSONObject object) {
        MdmModuleDistributeEntity entity = getData("jsonStr", object, MdmModuleDistributeEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity.setUpdate();
        mdmModuleDistributeDao.logicRemoveMultiCondition(entity);
        MdmModuleDistributeDetailEntity mdmModuleDistributeDetailEntity = new MdmModuleDistributeDetailEntity();
        mdmModuleDistributeDetailEntity.setDistributeId(entity.getId());
        mdmModuleDistributeDetailEntity.setUpdate();
        mdmModuleDistributeDetailDao.logicRemoveMultiCondition(mdmModuleDistributeDetailEntity);

        return BaseResult.getSuccessMessageEntity("删除数据来源成功");
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-写入日志-列表获取
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity queryMdmOptionLogPage(JSONObject object) {
        MdmModuleOptionLogEntity entity = getData("jsonStr", object, MdmModuleOptionLogEntity.class);
        //判断分页
        if (entity == null || entity.getPageNum() == null || entity.getPageSize() == null) {
            return BaseResult.getFailureMessageEntity("分页查询参数不存在");
        }
        if (entity.getMdmCode() == null || "".equals(entity.getMdmCode())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        MdmModuleEntity mdmModuleEntity = mdmServiceCache.getMdmModuleEntity(entity.getMdmCode());
        if (mdmModuleEntity == null || mdmModuleEntity.getId() == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
        mdmModuleDbEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleDbEntity.setSts("Y");
        List<MdmModuleDbEntity> mdmModuleDbEntities = mdmServiceCache.queryMdmModuleDb(mdmModuleDbEntity);
        if (mdmModuleDbEntities != null && mdmModuleDbEntities.size() > 0) {
            for (int i = 0; i < mdmModuleDbEntities.size(); i++) {
                if ("1".equals(mdmModuleDbEntities.get(i).getDbType())) {
                    entity.setTableName(mdmModuleDbEntities.get(i).getDbName() + "_option_log");
                    break;
                }
            }
        } else {
            return BaseResult.getFailureMessageEntity("请先设置数据源");
        }
        PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
        List<MdmModuleOptionLogEntity> list = mdmModuleOptionLogDao.queryByLike(entity);
        PageInfo pageInfo = new PageInfo(list);
        return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-下发日志-列表获取
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity queryMdmDistributeLogPage(JSONObject object) {
        MdmModuleSendLogEntity entity = getData("jsonStr", object, MdmModuleSendLogEntity.class);
        //判断分页
        if (entity == null || entity.getPageNum() == null || entity.getPageSize() == null) {
            return BaseResult.getFailureMessageEntity("分页查询参数不存在");
        }
        if (entity.getMdmCode() == null || "".equals(entity.getMdmCode())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        MdmModuleEntity mdmModuleEntity = mdmServiceCache.getMdmModuleEntity(entity.getMdmCode());
        if (mdmModuleEntity == null || mdmModuleEntity.getId() == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
        mdmModuleDbEntity.setMdmId(mdmModuleEntity.getId());
        mdmModuleDbEntity.setSts("Y");
        List<MdmModuleDbEntity> mdmModuleDbEntities = mdmServiceCache.queryMdmModuleDb(mdmModuleDbEntity);
        if (mdmModuleDbEntities != null && mdmModuleDbEntities.size() > 0) {
            for (int i = 0; i < mdmModuleDbEntities.size(); i++) {
                if ("1".equals(mdmModuleDbEntities.get(i).getDbType())) {
                    entity.setTableName(mdmModuleDbEntities.get(i).getDbName() + "_send_log");
                    break;
                }
            }
        } else {
            return BaseResult.getFailureMessageEntity("请先设置数据源");
        }
        PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
        List<MdmModuleSendLogEntity> list = mdmModuleSendLogDao.queryByLike(entity);
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getOptionType() != null) {
                    if ("1".equals(list.get(i).getOptionType())) {
                        list.get(i).setOptionType("新增");
                    } else if ("2".equals(list.get(i).getOptionType())) {
                        list.get(i).setOptionType("修改");
                    } else if ("3".equals(list.get(i).getOptionType())) {
                        list.get(i).setOptionType("删除");
                    }
                }
            }
        }
        PageInfo pageInfo = new PageInfo(list);
        return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-数据源-服务获取（只获取主表）
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity queryMdmService(JSONObject object) {
        MdmModuleDbEntity entity = getData("jsonStr", object, MdmModuleDbEntity.class);
        MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
        mdmModuleDbEntity.setSts("Y");
        mdmModuleDbEntity.setDbType("1");
        List<MdmModuleDbEntity> mdmModuleDbEntities = mdmModuleDbDao.queryBase(mdmModuleDbEntity);
        return BaseResult.getSuccessMessageEntity("查询数据成功", mdmModuleDbEntities);
    }


    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-数据源-服务字段获取
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity queryMdmServiceField(JSONObject object) {
        MdmModuleDbFiledsEntity entity = getData("jsonStr", object, MdmModuleDbFiledsEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getDbId() == null || "".equals(entity.getDbId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmId() == null || "".equals(entity.getMdmId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        MdmModuleDbFiledsEntity mdmModuleDbFiledsEntity = new MdmModuleDbFiledsEntity();
        mdmModuleDbFiledsEntity.setSts("Y");
        mdmModuleDbFiledsEntity.setMdmId(entity.getMdmId());
        List<MdmModuleDbFiledsEntity> mdmModuleDbEntities = mdmServiceCache.queryMdmModuleDbFileds(mdmModuleDbFiledsEntity);
        List<MdmModuleDbFiledsEntity> list = new ArrayList<>();
        if (mdmModuleDbEntities != null && mdmModuleDbEntities.size() > 0) {
            for (int i = 0; i < mdmModuleDbEntities.size(); i++) {
                if (entity.getDbId().equals(mdmModuleDbEntities.get(i).getDbId()) && "1".equals(mdmModuleDbEntities.get(i).getViewType())) {
                    list.add(mdmModuleDbEntities.get(i));
                }
            }
        }
        return BaseResult.getSuccessMessageEntity("查询数据成功", list);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-分发-只获取主表
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity queryMdmMainDB(JSONObject object) {
        MdmModuleDbEntity entity = getData("jsonStr", object, MdmModuleDbEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmId() == null || "".equals(entity.getMdmId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
        mdmModuleDbEntity.setSts("Y");
        mdmModuleDbEntity.setMdmId(entity.getMdmId());
        List<MdmModuleDbEntity> mdmModuleDbEntities = mdmServiceCache.queryMdmModuleDb(mdmModuleDbEntity);
        List<MdmModuleDbEntity> list = new ArrayList<>();
        if (mdmModuleDbEntities != null && mdmModuleDbEntities.size() > 0) {
            for (int i = 0; i < mdmModuleDbEntities.size(); i++) {
                if ("1".equals(mdmModuleDbEntities.get(i).getDbType())) {
                    list.add(mdmModuleDbEntities.get(i));
                }
            }
        }
        return BaseResult.getSuccessMessageEntity("查询数据成功", list);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-数据源-只获取主表字段
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity queryMdmMainDBField(JSONObject object) {
        MdmModuleDbEntity entity = getData("jsonStr", object, MdmModuleDbEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmId() == null || "".equals(entity.getMdmId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        MdmModuleDbEntity mdmModuleDbEntity = new MdmModuleDbEntity();
        mdmModuleDbEntity.setSts("Y");
        mdmModuleDbEntity.setMdmId(entity.getMdmId());
        List<MdmModuleDbEntity> mdmModuleDbEntities = mdmServiceCache.queryMdmModuleDb(mdmModuleDbEntity);

        MdmModuleDbFiledsEntity mdmModuleDbFiledsEntity = new MdmModuleDbFiledsEntity();
        mdmModuleDbFiledsEntity.setSts("Y");
        mdmModuleDbFiledsEntity.setMdmId(entity.getMdmId());
        List<MdmModuleDbFiledsEntity> mdmModuleDbFiledsEntities = mdmServiceCache.queryMdmModuleDbFileds(mdmModuleDbFiledsEntity);

        List<MdmModuleDbFiledsEntity> list = new ArrayList<>();
        if (mdmModuleDbEntities != null && mdmModuleDbEntities.size() > 0) {
            for (int i = 0; i < mdmModuleDbEntities.size(); i++) {
                if ("1".equals(mdmModuleDbEntities.get(i).getDbType())) {
                    if (mdmModuleDbFiledsEntities != null && mdmModuleDbFiledsEntities.size() > 0) {
                        for (int i1 = 0; i1 < mdmModuleDbFiledsEntities.size(); i1++) {
                            if (mdmModuleDbEntities.get(i).getId().equals(mdmModuleDbFiledsEntities.get(i1).getDbId()) && "1".equals(mdmModuleDbFiledsEntities.get(i1).getViewType())) {
                                list.add(mdmModuleDbFiledsEntities.get(i1));
                            }
                        }
                    }
                }
            }
        }
        return BaseResult.getSuccessMessageEntity("查询数据成功", list);
    }


    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 主数据-分发设置-列表获取
     * @Date 11:43 上午 2024/6/11
     **/
    @Override
    public JsonResultEntity queryMdmDistributeByMdmCode(JSONObject object) {
        MdmModuleDistributeEntity entity = getData("jsonStr", object, MdmModuleDistributeEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getMdmCode() == null || "".equals(entity.getMdmCode())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        MdmModuleEntity mdmModuleEntity = mdmServiceCache.getMdmModuleEntity(entity.getMdmCode());
        entity.setMdmId(mdmModuleEntity.getId());
        List<MdmModuleDistributeEntity> list = mdmModuleDistributeDao.queryBase(entity);
        return BaseResult.getSuccessMessageEntity("获取分发设置成功", list);
    }

    /**
     * 生成导入模版
     */
    @Override
    public void generateExcelTemplate(HttpServletResponse response) throws IOException {
        // 设置响应内容类型
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 设置文件名，注意中文需要URLEncoder编码
        String fileName = URLEncoder.encode("表字段定义模版", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        // 准备模板的示例数据
         List<ExcelTemplateVO> demoData = new ArrayList<>(); //createTemplateDemoData()
        // 使用EasyExcel直接写入响应输出流
        EasyExcel.write(response.getOutputStream(), ExcelTemplateVO.class)
                .sheet("表字段定义模版")
                .doWrite(demoData);
    }

    /*private List<ExcelTemplateVO> createTemplateDemoData() {
        List<ExcelTemplateVO> demoData = new ArrayList<>();
        ExcelTemplateVO demo = new ExcelTemplateVO();
        demo.setChName("用户ID");
        demo.setEnName("userId");
        demo.setFiledType("1"); // 对应BIGINT类型
        demo.setFiledLength("20");
        demo.setIsSysPk("Y");
        demo.setIsSysCode("Y");
        demo.setIsSysName("N");
        demo.setIsSysDate("N");
        demo.setTitle("用户ID");
        demo.setRow("6");
        demo.setWidth("100");
        demo.setType("input");
        demo.setRequired("true");
        demo.setDisabled("true");

        demoData.add(demo);
        return demoData;
    }
*/
    /**
     * 导入模版
     */
    @Transactional
    @Override
    public List<MdmModuleDbFiledsEntity> importTemplateFile(MultipartFile file , ImportExcelVO importExcelVO) {
        // 校验文件合法性
        if (file == null || file.isEmpty()) {
            throw new IllegalArgumentException("导入文件不能为空");
        }
        // 校验文件格式
        String fileName = file.getOriginalFilename();
        if (fileName == null || (!fileName.endsWith(".xlsx") && !fileName.endsWith(".xls"))) {
            throw new IllegalArgumentException("请上传Excel格式文件");
        }
        // 表头数据
        List<List<String>> headerList = new ArrayList<>();
        // 用于存储内容数据
        List<ExcelTemplateVO> dataList = new ArrayList<>();
        List<MdmModuleDbFiledsEntity> resultEntities = new ArrayList<>();
        try (InputStream inputStream = file.getInputStream()) {
            // 使用EasyExcel读取文件
            EasyExcel.read(inputStream)
                    .head(ExcelTemplateVO.class)  // 指定映射的实体类
                    .sheet(0)            // 指定第一个sheet页
                    .headRowNumber(1)            // 表头行数（从1开始）
                    .autoTrim(true)
                    .registerReadListener(new AnalysisEventListener<ExcelTemplateVO>() {
                        /**
                         * 获取表头行数据
                         */
                        @Override
                        public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
                            // 1. 转换表头为有序列表
                            List<String> header = new ArrayList<>();
                            // 获取最大列索引，确保顺序正确
                            int maxColIndex = headMap.keySet().stream().mapToInt(Integer::intValue).max().orElse(-1);
                            for (int i = 0; i <= maxColIndex; i++) {
                                header.add(headMap.getOrDefault(i, ""));  // 无数据的列填空
                            }
                            headerList.add(header);
                        }
                        /**
                         * 每解析一行数据触发
                         */
                        @Override
                        public void invoke(ExcelTemplateVO data, AnalysisContext context) {
                            // 空行直接跳过
                            if (isRowEmpty(data)) {
                                return;
                            }
                            // 数据校验
                            validateData(data, context.readRowHolder().getRowIndex() + 1);
                            dataList.add(data);
                        }
                        /**
                         * 所有数据解析完成后触发
                         */
                        @Override
                        public void doAfterAllAnalysed(AnalysisContext context) {
                            // 解析完成后的数据处理
                            List<MdmModuleDbFiledsEntity> entities = processImportData(headerList, dataList, importExcelVO);
                            if (entities != null) {
                                resultEntities.addAll(entities);
                            }
                        }
                    })
                    .doRead();  // 执行读取操作
        } catch (IOException e) {
            throw new RuntimeException("文件读取失败：" + e.getMessage(), e);
        }
        return resultEntities;
    }

    private boolean isRowEmpty(ExcelTemplateVO data) {
        if(data == null) {
            return true;
        }
        return  StringUtils.isEmpty(data.getChName() ) &&
                StringUtils.isEmpty(data.getFiledType()) &&
                StringUtils.isEmpty(data.getEnName()) ;
    }

    /**
     * 导入数据，新增字段
     * @param headerList
     * @param dataList
     */
    private List<MdmModuleDbFiledsEntity> processImportData(List<List<String>> headerList, List<ExcelTemplateVO> dataList,ImportExcelVO importExcelVO) {
        // 获取表头列名
        List<String> headers = headerList.get(0);
        // 数据库字段数据
        List<MdmModuleDbFiledsEntity> entities = new ArrayList<>();
        for (ExcelTemplateVO data : dataList) {
            MdmModuleDbFiledsEntity entity = new MdmModuleDbFiledsEntity();
            BeanUtils.copyProperties(data, entity);
            // 字段规则数据集合
            List<MdmModuleDbFiledsRuleEntity> rules = new ArrayList<>();
            for (String header : headers) {
                // 字段规则数据
                MdmModuleDbFiledsRuleEntity rule = new MdmModuleDbFiledsRuleEntity();
                rule.setRuleType("2");
                rule.setFormName("optionRuleForm");
                switch (header) {
                    case "显示名":
                        rule.setRuleName("显示名");
                        rule.setRuleCode("title");
                        rule.setRuleValue(data.getChName());
                        break;
                    case "宽度":
                        rule.setRuleName("宽度");
                        rule.setRuleCode("row");
                        rule.setRuleValue(data.getRow());
                        break;
                    case "单元格宽度":
                        rule.setRuleName("单元格宽度");
                        rule.setRuleCode("width");
                        rule.setRuleValue(data.getWidth());
                        break;
                    case "数据类型":
                        rule.setRuleName("数据类型");
                        rule.setRuleCode("type");
                        rule.setRuleValue(data.getType());
                        break;
                    case "必填":
                        rule.setRuleName("必填");
                        rule.setRuleCode("required");
                        rule.setRuleValue(data.getRequired() != null ? "true" : "false");
                        break;
                    case "禁止修改":
                        rule.setRuleName("禁止修改");
                        rule.setRuleCode("disabled");
                        rule.setRuleValue(data.getDisabled() != null ? "true" : "false");
                        break;
                    default:
                        continue;
                }
                rules.add(rule);
            }
            entity.setMdmModuleDbFiledsRules(rules);
            entity.setDbName(importExcelVO.getDbName());
            entity.setDbId(importExcelVO.getDbId());
            entity.setDbType(importExcelVO.getDbType());
            entity.setMdmId(importExcelVO.getMdmId());
            entities.add(entity);
        }
        return entities;
    }

    /**
     * 数据校验方法，验证Excel导入的数据合法性
     * @param data 导入的Excel数据对象
     * @param rowNum 行号
     */
    private void validateData(ExcelTemplateVO data, int rowNum) {
        List<String> errors = new ArrayList<>();

        // 1. 校验中文名称必填
        if (StringUtils.isBlank(data.getChName())) {
            errors.add("第" + rowNum + "行：中文名称不能为空");
        }

        // 2. 校验字段类型合法性
        if (StringUtils.isBlank(data.getFiledType())) {
            errors.add("第" + rowNum + "行：字段类型不能为空");
        } else {
            Set<String> validTypes = new HashSet<>(Arrays.asList("1", "2", "3", "4"));
            if (!validTypes.contains(data.getFiledType())) {
                errors.add("第" + rowNum + "行：字段类型无效，允许值为1(BIGINT)、2(DECIMAL)、3(VARCHAR)、4(DATETIME)");
            }
        }

        // 3. 校验长度（数字类型且当字段类型为1、2、3时必填）
        if (StringUtils.isNotBlank(data.getFiledType()) &&
                (data.getFiledType().equals("1") || data.getFiledType().equals("2") || data.getFiledType().equals("3"))) {

            if (StringUtils.isBlank(data.getFiledLength())) {
                errors.add("第" + rowNum + "行：字段长度不能为空");
            } else if (!StringUtils.isNumeric(data.getFiledLength())) {
                errors.add("第" + rowNum + "行：字段长度必须为数字");
            } else {
                int length = Integer.parseInt(data.getFiledLength());
                if (length <= 0) {
                    errors.add("第" + rowNum + "行：字段长度必须大于0");
                }
            }
        }

        // 4. 校验系统主键标识合法性
        if (StringUtils.isNotBlank(data.getIsSysPk()) &&
                !"Y".equals(data.getIsSysPk()) && !"N".equals(data.getIsSysPk())) {
            errors.add("第" + rowNum + "行：是否系统主键只能为Y或N");
        }

        // 5. 校验系统数据编码标识合法性
        if (StringUtils.isNotBlank(data.getIsSysCode()) &&
                !"Y".equals(data.getIsSysCode()) && !"N".equals(data.getIsSysCode())) {
            errors.add("第" + rowNum + "行：是否系统数据编码只能为Y或N");
        }

        // 6. 校验系统数据名称标识合法性
        if (StringUtils.isNotBlank(data.getIsSysName()) &&
                !"Y".equals(data.getIsSysName()) && !"N".equals(data.getIsSysName())) {
            errors.add("第" + rowNum + "行：是否系统数据名称只能为Y或N");
        }

        // 7. 校验系统日期标识合法性
        if (StringUtils.isNotBlank(data.getIsSysDate()) &&
                !"Y".equals(data.getIsSysDate()) && !"N".equals(data.getIsSysDate())) {
            errors.add("第" + rowNum + "行：是否系统日期只能为Y或N");
        }

        // 8. 校验是否必填标识合法性
        if (data.getRequired() != null && !data.getRequired().trim().isEmpty()) {
            String required = data.getRequired().trim();
            if (!"TRUE".equalsIgnoreCase(required) && !"FALSE".equalsIgnoreCase(required)) {
                errors.add("第" + rowNum + "行：是否必填只能为true或false");
            }
        }

        // 9. 校验禁止修改标识合法性
        if (data.getDisabled() != null && !data.getDisabled().trim().isEmpty()) {
            String disabled = data.getDisabled().trim();
            if (!"TRUE".equalsIgnoreCase(disabled) && !"FALSE".equalsIgnoreCase(disabled)) {
                errors.add("第" + rowNum + "行：禁止修改只能为true或false");
            }
        }

        // 如果有错误，抛出异常中断导入
        if (!errors.isEmpty()) {
            throw new ExcelDataValidateException(String.join("；", errors));
        }
    }

    // 自定义异常类
    public class ExcelDataValidateException extends RuntimeException {
        public ExcelDataValidateException(String message) {
            super(message);
        }
    }




}
